home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16445 < prev    next >
Encoding:
Text File  |  1992-11-17  |  2.0 KB  |  49 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!sdd.hp.com!spool.mu.edu!agate!doc.ic.ac.uk!daresbury!mrccrc!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: operator overloading
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov17.184136.1@vax1.bham.ac.uk>
  7. Date: Tue, 17 Nov 1992 18:41:36 GMT
  8. Lines: 37
  9. References: <1992Nov16.200145.23491@aifh.ed.ac.uk> <Bxu3DB.F7n@news.cso.uiuc.edu>
  10. Organization: University of Birmingham
  11.  
  12. In article <Bxu3DB.F7n@news.cso.uiuc.edu>, pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes:
  13. > In <1992Nov16.200145.23491@aifh.ed.ac.uk> iab@cstr.ed.ac.uk writes:
  14. >>I'm looking for some help with operator overloading. I have
  15. >>successfully overloaded operators acting on statically declared  user
  16. >>defined classes but I would like to declare my classes dynamically and
  17. >>still use my overloaded operators. Is this possible, i.e. to use
  18. >>overloaded operators on pointers to a class without dereferencing
  19. >>explicitly.     
  20. > *****>    No; pointers are not first-class objects; operators can only be
  21. >     overloaded for class types.
  22.  
  23. Unless of, course, you make them 1st-class :-)
  24.  
  25. template <class T> Ptr { 
  26.   T* ptr;  
  27. public:
  28.   Ptr(T* p) : ptr(p) {}
  29.   operator T* () {return ptr;}
  30.   T* operator->() {return *this;}
  31. };
  32.  
  33. Now if you use variables of type Ptr<T> can now be used in place of T* and
  34. you can define operators using Ptr classes. If you don't have templates then
  35. you'll need to define one Ptr class for each class by hand.
  36.  
  37. >     You might consider using references whereever you can.
  38. Yes. If you can manage to do it this way it's probably better.
  39. -- 
  40.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  41.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  42. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  43. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  44.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  45. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  46.