home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!pipex!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: <1992Nov20.191832.1@vax1.bham.ac.uk>
- Date: Fri, 20 Nov 1992 19:18:32 GMT
- Lines: 53
- References: <1ehr3gINNopf@gap.caltech.edu>
- Organization: University of Birmingham
-
- In article <1ehr3gINNopf@gap.caltech.edu>, iotov@ccsf.caltech.edu (Mihail Iotov) writes:
- > I am trying to overload some operators this way
- > set& set::operator= (const set& s) ;
- > set set::operator* (const set& s) {
- > set product ;
- > [do something]
- > return product ; /* this line is 88 */
- > }
- > and I get form g++ :
- >
- > set.cxx: In method class set set::operator * (const class set &):
- > set.cxx:88: warning: bitwise copy: `set' defines operator=()
- >
- > Shouldn't the assignment method be called for the return operation ?
- >
- No, it should call the copy constructor. (Wow what an impressive warning to
- tell you that you have a self-assignment operator but not a copy ctor.
- Congrats GNU :-) :-) )
-
- The assignment operator is only called by assignment, ie when an _existing_
- object has it's value changed. When the object does not already exist
- then it is the copy-ctor that is called.
-
- The copy ctor is needed whenever an object is passed or returned by value.
- This is because the object may get moved in memory as a result of the
- change of scope. The copy ctor tells the complier what book keeping is
- required to move the object. (The old object is destroyed immediately
- after creating the new one.)
- (Actually strictly speaking I think it _may_ be needed - the compiler is
- encouraged to omit the call if it can manage it. I think in some really
- strange situations it may need to call it more than once.
- (Someone please correct me if I'm wrong.))
-
- So given;
-
- set a,b,c;
- a=b*c;
-
- The temporary `set' object that is the argument to set::operator(const set&)
- is created by calling set::set(const set&). If set::set(const set&) does
- not exist then a bitwise copy is used by default but this is not usually
- safe for a class with a self-assignment operator.
-
- >
- > Thanks,
- > Mihail
- --
- \\ ( ) 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
-