By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,805 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

explicit copy constructor

P: n/a
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 {
Base() {
Base(const Base& other) {

template<typename T>
class Derived: public Base {

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

Aug 10 '07 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On 8 10 , 11 53 , "Alf P. Steinbach" <al...@start.nowrote:
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<Uto

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

Aug 10 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.