home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
- Subject: Re: operator overloading
- Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
- Message-ID: <1992Nov17.184136.1@vax1.bham.ac.uk>
- Date: Tue, 17 Nov 1992 18:41:36 GMT
- Lines: 37
- References: <1992Nov16.200145.23491@aifh.ed.ac.uk> <Bxu3DB.F7n@news.cso.uiuc.edu>
- Organization: University of Birmingham
-
- In article <Bxu3DB.F7n@news.cso.uiuc.edu>, pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes:
- > In <1992Nov16.200145.23491@aifh.ed.ac.uk> iab@cstr.ed.ac.uk writes:
- >
- >
- >>I'm looking for some help with operator overloading. I have
- >>successfully overloaded operators acting on statically declared user
- >>defined classes but I would like to declare my classes dynamically and
- >>still use my overloaded operators. Is this possible, i.e. to use
- >>overloaded operators on pointers to a class without dereferencing
- >>explicitly.
- >
- > *****> No; pointers are not first-class objects; operators can only be
- > overloaded for class types.
-
- Unless of, course, you make them 1st-class :-)
-
- template <class T> Ptr {
- T* ptr;
- public:
- Ptr(T* p) : ptr(p) {}
- operator T* () {return ptr;}
- T* operator->() {return *this;}
- };
-
- Now if you use variables of type Ptr<T> can now be used in place of T* and
- you can define operators using Ptr classes. If you don't have templates then
- you'll need to define one Ptr class for each class by hand.
-
- > You might consider using references whereever you can.
- Yes. If you can manage to do it this way it's probably better.
- --
- \\ ( ) No Bullshit! | Email: B.A.McCauley@bham.ac.uk
- . _\\__[oo from | Voice: +44 21 471 3789 (home)
- .__/ \\ /\@ /~) /~[ /\/[ | Fax: +44 21 625 2175 (work)
- . l___\\ /~~) /~~[ / [ | Snail: 197 Harborne Lane,
- # ll l\\ ~~~~ ~ ~ ~ ~ | Birmingham, B29 6SS, UK
- ###LL LL\\ (Brian McCauley) | ICBM: 52.5N 1.9W
-