?
=?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.
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.