home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.c++
- Subject: Re: Default assignment semantics
- Date: 14 Sep 1992 19:06:03 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 38
- Distribution: usa
- Message-ID: <192nqrINN8tb@early-bird.think.com>
- References: <BuJs8K.Hzr@vcd.hp.com> <1992Sep14.152055.5825@sunb10.cs.uiuc.edu>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Sep14.152055.5825@sunb10.cs.uiuc.edu> pjl@sparc10.cs.uiuc.edu (Paul Lucas) writes:
- >>Thus what I want to do in most of my assignment operators is
- >>explicitly invoke the corresponding destructor and copy constructor.
- >
- >*****> Ah..."most." There are occasions, as I've already pointed out,
- > when one doesn't want the d'tor called. To have this
- > flexibility is probably part of the reason the assignment
- > operator is as it is.
-
- No one is suggesting taking that flexibility away from you.
-
- I like the original poster's idea. Why force the programmer to write three
- functions when the compiler can often infer the third one from the first
- two? Sure, it may be possible to do better than calling the destructor and
- copy constructor, but the current default assignment operator is almost
- always wrong when these are defined.
-
- If the language isn't changed, I would suggest that compiler implementors
- add a warning when they see a class with a copy constructor and destructor,
- but no assignment operator.
-
- On a related note, I think that something needs to be done about the fact
- that assignment operators aren't inherited. I think the default assignment
- operator for a class derived from classes with user-defined assignment
- operators, and which doesn't have its own copy constructor or destructor
- (so that it doesn't fall into the above case) should be to invoke the base
- classes' assignment operators and then perform memberwise assignment on the
- members that it adds. And if this isn't done, I think compilers should
- warn when a class derives from a base with an assignment operator but
- doesn't also overload assignment; if a base class needs special assignment
- semantics, it seems pretty unlikely that the default semantics would be
- appropriate for the derived class.
-
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-