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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!pipex!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: <1992Nov20.191832.1@vax1.bham.ac.uk>
  7. Date: Fri, 20 Nov 1992 19:18:32 GMT
  8. Lines: 53
  9. References: <1ehr3gINNopf@gap.caltech.edu>
  10. Organization: University of Birmingham
  11.  
  12. In article <1ehr3gINNopf@gap.caltech.edu>, iotov@ccsf.caltech.edu (Mihail Iotov) writes:
  13. > I am trying to overload some operators this way
  14. > set&   set::operator= (const set& s) ;
  15. > set  set::operator* (const set& s)  {
  16. >   set   product ;
  17. >   [do something]
  18. >   return product ;        /* this line is 88 */
  19. > }
  20. > and I get form g++ :
  21. > set.cxx: In method class set set::operator * (const class set &):
  22. > set.cxx:88: warning: bitwise copy: `set' defines operator=()
  23. > Shouldn't the assignment method be called for the return operation ?
  24. >
  25. No, it should call the copy constructor. (Wow what an impressive warning to
  26. tell you that you have a self-assignment operator but not a copy ctor.
  27. Congrats GNU :-) :-) )
  28.  
  29. The assignment operator is only called by assignment, ie when an _existing_
  30. object has it's value changed. When the object does not already exist 
  31. then it is the copy-ctor that is called.
  32.  
  33. The copy ctor is needed whenever an object is passed or returned by value.
  34. This is because the object may get moved in memory as a result of the 
  35. change of scope. The copy ctor tells the complier what book keeping is
  36. required to move the object. (The old object is destroyed immediately
  37. after creating the new one.)
  38. (Actually strictly speaking I think it _may_ be needed - the compiler is
  39. encouraged to omit the call if it can manage it. I think in some really
  40. strange situations it may need to call it more than once. 
  41. (Someone please correct me if I'm wrong.))
  42.  
  43. So given;
  44.  
  45.   set a,b,c;
  46.   a=b*c; 
  47.  
  48. The temporary `set' object that is the argument to set::operator(const set&)
  49. is created by calling set::set(const set&). If set::set(const set&) does
  50. not exist then a bitwise copy is used by default but this is not usually
  51. safe for a class with a self-assignment operator.
  52.  
  53. > Thanks, 
  54. >     Mihail
  55. -- 
  56.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  57.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  58. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  59. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  60.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  61. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  62.