explicit copy constructor

  • Thread starter =?gb2312?B?wfXquw==?=
  • Start date
?

=?gb2312?B?wfXquw==?=

Hi, folks,

I am trying to make my copy constructor explicit, but when the
scenario below comes into being, weird things happen.
Here is the code snippet:


#include <iostream>
using namespace std;

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

template<typename T>
class Derived: public Base {
public:

Derived() {
}

template<typename U>
Derived(const Derived<U>& other): Base(*(Base*)&other) {
cout << "Cast copy constructor\n";
}

//here is the trouble maker
explicit Derived(const Derived& other): Base(*(Base*)&other) {
cout << "Normal copy constructor\n";
}
};

int main() {

Derived<int> d1;

Derived<int> d2 = d1;

return 0;
}

Intuitively, I want "Normal copy constructor" printed, but what I saw
is "Cast copy constructor", while I remove "explicit" keyword,
everything is OK, the output is "Normal copy constructor", any
informative tips will receive my big thanks.
 
A

Alf P. Steinbach

* ??:
Hi, folks,

I am trying to make my copy constructor explicit, but when the
scenario below comes into being, weird things happen.
Here is the code snippet:


#include <iostream>
using namespace std;

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

template<typename T>
class Derived: public Base {
public:

Derived() {
}

template<typename U>
Derived(const Derived<U>& other): Base(*(Base*)&other) {
cout << "Cast copy constructor\n";
}

//here is the trouble maker
explicit Derived(const Derived& other): Base(*(Base*)&other) {
cout << "Normal copy constructor\n";
}
};

int main() {

Derived<int> d1;

Derived<int> d2 = d1;

return 0;
}

Intuitively, I want "Normal copy constructor" printed, but what I saw
is "Cast copy constructor", while I remove "explicit" keyword,
everything is OK, the output is "Normal copy constructor", any
informative tips will receive my big thanks.

An explicit constructor must be called explictly.

You don't call it explicitly, so how can you expect it to be called?

Apart from that, first, you really don't want to use reinterpret_cast as
you do above, and secondly, the templated constructor isn't a copy
constructor -- a "copy constructor" is by definition not templated.
 
G

Guest

An explicit constructor must be called explictly.
You don't call it explicitly, so how can you expect it to be called?

So, you mean if I define a explicit copy constructor, there can not be
any temporary object in my code, right?

Apart from that, first, you really don't want to use reinterpret_cast as
you do above, and secondly, the templated constructor isn't a copy
constructor -- a "copy constructor" is by definition not templated.

The templated constructor is used for type casting from Derived<U> to
Derived<T>.

Thanks for your informative tips.
Big thanks goes to you.
 
A

Alf P. Steinbach

* (e-mail address removed):
So, you mean if I define a explicit copy constructor, there can not be
any temporary object in my code, right?

No, I don't mean that. A temporary object need not be copied. In
particular, you can call member functions on a temporary object.

The templated constructor is used for type casting from Derived<U> to
Derived<T>.

Yes, and you really don't want to use reinterpret_cast for that,
especially not in the disguise of a C-style cast.

Thanks for your informative tips.
Big thanks goes to you.

You're welcome.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* (e-mail address removed):

No, I don't mean that. A temporary object need not be copied. In
particular, you can call member functions on a temporary object.



Yes, and you really don't want to use reinterpret_cast for that,
especially not in the disguise of a C-style cast.

Sorry, it isn't a reinterpret_cast you have: it's a completely
unnecessary cast (I was reading some meaning into it, there isn't any).

Just remove the cast.

Any Derived<T>& is implicitly convertible to Base&.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top