explicit copy constructor

Discussion in 'C++' started by =?gb2312?B?wfXquw==?=, Aug 10, 2007.

  1. 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.
     
    =?gb2312?B?wfXquw==?=, Aug 10, 2007
    #1
    1. Advertising

  2. * ??:
    > 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.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 10, 2007
    #2
    1. Advertising

  3. =?gb2312?B?wfXquw==?=

    Guest Guest

    On 8 10 , 11 53 , "Alf P. Steinbach" <> wrote:
    > 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.
     
    Guest, Aug 10, 2007
    #3
  4. * :
    > On 8 10 , 11 53 , "Alf P. Steinbach" <> wrote:
    >> 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?


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


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


    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: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 10, 2007
    #4
  5. * Alf P. Steinbach:
    > * :
    >> On 8 10 , 11 53 , "Alf P. Steinbach" <> wrote:
    >>> 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?

    >
    > No, I don't mean that. A temporary object need not be copied. In
    > particular, you can call member functions on a temporary object.
    >
    >
    >>> 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>.

    >
    > 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&.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 10, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dario
    Replies:
    2
    Views:
    632
    tom_usenet
    Sep 22, 2003
  2. Jean Stax
    Replies:
    4
    Views:
    483
    lilburne
    Nov 23, 2003
  3. Christoph Bartoschek

    Explicit copy constructor

    Christoph Bartoschek, Apr 27, 2004, in forum: C++
    Replies:
    6
    Views:
    6,774
    Christoph Bartoschek
    Apr 28, 2004
  4. trying_to_learn
    Replies:
    8
    Views:
    4,790
    Victor Bazarov
    Nov 18, 2004
  5. cinsk
    Replies:
    35
    Views:
    2,733
    James Kanze
    Oct 11, 2010
Loading...

Share This Page