Topic: Is Derived::Derived(const Base&) a copy ctor?


Author: jason@cygnus.com (Jason Merrill)
Date: 1995/11/15
Raw View
>>>>> jodle  <jodle@bix.com> writes:

> Wil Evers (wil@ittpub.nl) wrote:
> : ..then Derived will not have a generated copy constructor, since
> : Derived::Derived(const Base&) is a constructor that can be called with a
> : single argument of type const Derived&.

> In any case, Derived will not have a generated copy constructor since a
> constructor (of any form) was declared in the class.

This is incorrect.  The synthesized copy ctor is only suppressed by another
copy ctor; it's the synthesized default ctor that is suppressed by any
other ctor.

Jason

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]






Author: wil@ittpub.nl (Wil Evers)
Date: 1995/11/01
Raw View

I'm trying to figure out exactly when a constructor is a copy constructor.
The ARM, section section 12.1, says:

"A copy constructor for a class X is a constructor that can be called to
copy an object of class X; that is, one that can be called with a single
argument of type X."

In my interpretation, this means that if I write...

class Base { };
class Derived : public Base { public : Derived(const Base&) { } };

..then Derived will not have a generated copy constructor, since
Derived::Derived(const Base&) is a constructor that can be called with a
single argument of type const Derived&.

In contrast, the DWP says (12.8.2, [class.copy])

"A constructor for class X is a copy constructor if its first parameter is
of type X& or const X&, and either there are no other parameters or all
other parameters have default arguments."

which is different, because Derived::Derived(const Base&) does not have a
first parameter of type Derived& or const Derived&.

Is this a deliberate change from the definition in the ARM? If so, does
anyone know what the rationale behind this change is? (Personally, I think
the definition in the DWP is preferable because I occasionally need to
write such a constructor to `upgrade' a base class object to a derived
class object and I got bitten once or twice because the compiler wouldn't
generate a copy constructor for the derived class if I did so. Still, I
can also imagine code that would break because of this change).

Thanks!

- Wil

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]