Topic: Copy ctors use of oper=
Author: walke751.NOSPAM@concentric.net (Daryle Walker)
Date: 1998/12/16 Raw View
In article <3675FB04.363A624B@xroads.com>, David Wieringa
<wieringa@xroads.com> wrote:
>During the same walk-thru it came up that one should not all the
>classes' assignment operator from the copy constructor (i.e. copy
>constructor of *this = sourceObject). Is there any validy to this?
>
>If this is true, why doesn't the compiler catch this? (or do most of
>them?)
>
>Should one in fact duplicate the assignment code in the copy constructor
>if this is what is needed?
It all depends on the nature of the class's components. Even though they
are similar in action, you should treat (copy) construction and (plain)
assignment as conceptually different.
1. If you are using simple types (the built-in numerical/bool/char types,
structures made up of only these types or other structures that follow
this rule), using assignment for copy-construction is OK, since bitwise
copies were all that were involved anyway. (Allowing this case prevents
any compiler from complaining.)
2. If you just use assignment, all the class members with the ability get
default-constructed anyway, only to (possibly) get the default values
wiped out by the assignment. For some members, the extra construction
(and destruction) may incur a significant time penalty.
3. You may have to use some member initializations, for members where
assignment differs from construction. (i.e. references, constants,
constant references, other members without default constructors, and
parent classes without default constructors)
4. An assignment operator may assume that the left operand is already a
properly-constructed object. This is not the case at the beginning of any
constructor. So unless you take care with the default constructions
and/or member initialization list, the assignment operator may choke on
members with invalid/unexpected garbage values (like pointers).
5. Some poster made the point of declaring the assignment operator
correctly. Make sure that operator= takes its right operand by (constant)
reference. If it uses by-value, then operator= would need to call the
copy constructor (which would call operator=, ...). This would result in
infinite recursion.
6. Using the assignment operator in the copy constructor should be OK for
derived classes, as long as it works for the original class. (IMO, this
is because in a copy constructor using "*this = sourceObject", the derived
portions of *this haven't been considered yet [and can't be since that
would require the constructor to know which derived class is using it] and
the derived parts of sourceObject are ignored.)
7. If all you want is to not duplicate code in the assignment operator and
the copy constructor, just segregate the common code to another (private)
method.
Interestingly, there is a similar thread with someone wanting to do the
reverse of what you suggested. He suggested using [the destructor
explicitly then calling] the copy constructor to implement the assignment
operator.
--
Daryle Walker
Video Game, Mac, and Internet Junkie
walke751 AT concentric DOT net
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: abrahams@spam.motu.com (David Abrahams)
Date: 1998/12/16 Raw View
On 16 Dec 1998 07:10:44 GMT, walke751.NOSPAM@concentric.net (Daryle
Walker) wrote:
>Interestingly, there is a similar thread with someone wanting to do the
>reverse of what you suggested. He suggested using [the destructor
>explicitly then calling] the copy constructor to implement the assignment
>operator.
An extremely dangerous practice likely to lead to objects being
destroyed twice if the copy constructor throws an exception!
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/12/16 Raw View
In article <3677dfe4.1384299694@news.motu.com>,
abrahams@spam.motu.com wrote:
>
> On 16 Dec 1998 07:10:44 GMT, walke751.NOSPAM@concentric.net (Daryle
> Walker) wrote:
>
> >Interestingly, there is a similar thread with someone wanting to do the
> >reverse of what you suggested. He suggested using [the destructor
> >explicitly then calling] the copy constructor to implement the assignment
> >operator.
>
> An extremely dangerous practice likely to lead to objects being
> destroyed twice if the copy constructor throws an exception!
Also quite dangerous if there can be classes derived from this one.
Der d1, d2;
d1 = d2;
This might result in destroying the Der object d1, and then
constructing a Base object in it's place!
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]