home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13700 < prev    next >
Encoding:
Text File  |  1992-09-15  |  1.7 KB  |  44 lines

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. ReplyTo: nikki@trmphrst.demon.co.uk
  5. Subject: Re: Default assignment semantics
  6. References: <BuJs8K.Hzr@vcd.hp.com>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.16 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 29
  11. Date: Tue, 15 Sep 1992 11:43:17 +0000
  12. Message-ID: <716582597snx@trmphrst.demon.co.uk>
  13. Sender: usenet@gate.demon.co.uk
  14.  
  15.  
  16. In article <BuJs8K.Hzr@vcd.hp.com> jm@vcd.hp.com (John Matthews) writes:
  17.  
  18. > As I understand it, if a class does not have an explicitly defined
  19. > assignment operator, a default assignment operator is generated
  20. > by the compiler. This default operator simply does a member-wise
  21. > assignment. As has been pointed out many times, these semantics
  22. > are dangerous for classes that have pointers to dynamically created
  23. > objects as fields.
  24. > It seems to me that a more robust default assignment operator would
  25. > have the following semantics:
  26. >   1) Invoke the destructor of the object being assigned to.
  27. >   2) Invoke the copy constructor on the object being assigned
  28. >       to, taking as an argument the object being assigned from.
  29. The problem with this is that it introduces the most common assignment 
  30. operator bug. Try it with ...
  31.  
  32. String a"(Hello");
  33.  
  34. a = a;
  35. ---
  36. Nikki Locke              |                        | nikki@trmphrst.demon.co.uk
  37. Trumphurst Ltd.          | Tel: +44 (0)691-670318 | nikki@cix.compulink.co.uk
  38. PC and Unix consultancy  | Fax: +44 (0)691-670316 | nikki@kewill.co.uk
  39. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  40. Demon.co.uk is a dial-up subscription access point to the Internet.
  41.