Topic: About copy constructor and copy assignments...


Author: vandevod@pleiades.cs.rpi.edu (David Vandevoorde)
Date: 1995/04/15
Raw View
While writing a little piece of code for pedagogical purposes,
I found that the "definition" of copy constructor and copy
assignment clashed with my intuition. I'm not sure if this has
been discussed before, but comments are appreciated.

[this relates to par. 11.4.4 in D&E and 12.8 in the draft proposal]

Consider:

 struct B {
     // ...
 };

 struct D: B {
     D(const B&);  // Not a copy-constructor.
     D& operator=(const B&); // Not a copy-assignment
     // ...   // operator.
 };
 // Assume D does not define any copy-constructor or
 // copy assignment operator.

 void f() {
     D d;
     D dc = d; // Will use implicitly declared copy constructor,
        // not D(const B&).
     d = dc;   // Will use implicitly declared copy assignment
        // (i.e., memberwise).
 }

I would think that under these circumstances D::D(const B&) and
D::operator(const B&) should be considered copy-constructor and
copy assignment operators respectively. This would support the
notion (mentioned in D&E, par. 11.4.4 in the context of "slicing")
that a derived object should always be usable in place of a base
object.

 Daveed