🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Wil Evers Wed, 01 Nov 1995 18:38
Is Derived::Derived(const Base&) a copy ctor?
#2383
Author: Wil Evers
Date: Wed, 01 Nov 1995 18:38
43 lines
2111 bytes

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. ]
Re: Is Derived::Derived(const Base&) a copy ctor?
#2651
Author: Pablo Halpern
Date: Mon, 20 Nov 1995 00:03
58 lines
3157 bytes
wil@ittpub.nl (Wil Evers) wrote:

>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 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."
>
...
>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).

Actually, I prefer the ARM version specifically because of the issue we
are discussing in the thread about auto_ptr<>. If you want the argument
to your copy constructor going through some sort of intermediate proxy
conversion, then you don't want that work undone by the compiler:

template <class T>
class auto_ptr<T>
{
  class proxy { ... };
  ...
  operator proxy();	// conversion to proxy<T>
  auto_ptr<T>(const proxy&);  // line 3
  ...
};

According to the ARM, line 3 declares a copy constructor because you can
call it with an auto_ptr<T> argument. The compiler will not generate a
copy constructor for you. This is good, in this case. But according to
the DWP, line 3 does NOT declare a copy constructor and all of your
lovely work is lost as the compiler generates its own (incorrect) copy
constructor. Declaring a private copy constructor does not help because
the compiler still sees the copy constructor and just makes it illegal
to try to use it.

I can always manually make a copy constructor that does the same thing
that the compiler-generated one would do, but I can't always suppress
the compiler's urge to do something for me.
-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com


[ 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads