Copy Constructor and explicit attribute

Discussion in 'C++' started by Jean Stax, Nov 22, 2003.

  1. Jean Stax

    Jean Stax Guest

    Hi !

    I tried to understand when the explicit attribute in copy constructor
    prevents from
    me to create a new object. Bellow is the sample code.
    While the two first cases really generate a compilation error, the
    third (mc2 = Foo1();) compiles and runs without any problem. I am
    wondering why the:

    MyClass Foo1()
    {
    MyClass mc(1);
    return mc;
    }

    isn't forbidden, when MyClass copy constructor is defined as explicit.

    Thanks.


    class MyClass
    {
    public:
    MyClass(int nVal) : m_nVal1(nVal){}
    MyClass() : m_nVal1(0){}
    explicit MyClass(const MyClass& copy)
    {
    //...
    }
    private:
    int m_nVal1;
    };

    void Foo(MyClass cc)
    {
    }

    MyClass Foo1()
    {
    MyClass mc(1);
    return mc;
    }

    int main(int argc, char* argv[])
    {
    MyClass mc(1);
    MyClass mc1 = mc;
    //error C2440: 'initializing' : cannot convert from 'class MyClass' to
    //class MyClass' No copy constructor available for class 'MyClass'

    Foo(mc);
    //error C2664: 'Foo' : cannot convert parameter 1 from 'class MyClass'
    //to 'class MyClass'No copy constructor available for class 'MyClass'

    MyClass mc2;
    mc2 = Foo1();


    return 0;
    }
     
    Jean Stax, Nov 22, 2003
    #1
    1. Advertising

  2. Jean Stax

    lilburne Guest

    Jean Stax wrote:
    > Hi !
    >
    > I tried to understand when the explicit attribute in copy constructor
    > prevents from
    > me to create a new object. Bellow is the sample code.
    > While the two first cases really generate a compilation error, the
    > third (mc2 = Foo1();) compiles and runs without any problem. I am
    > wondering why the:
    >


    In neither case where you have an error is the instance of
    MyClass a const, and you have said that the compiler is not
    allowed to implicitely cast from non-const to const when
    using the copy-ctor.
     
    lilburne, Nov 22, 2003
    #2
    1. Advertising

  3. Jean Stax

    Jean Stax Guest

    Unfortunatelly, I can't see how your point makes the difference:

    when I change my code to (the explicit attribute was removed):

    MyClass(const MyClass& copy)
    {
    //...
    }

    my code get compilled succesfully.

    However, when I change my code to (const was removed):

    explicit MyClass(MyClass& copy)
    {
    //...
    }

    I still can't compile the first two cases.

    Thanks.

    lilburne <> wrote in message news:<bpoof7$1qktci$-berlin.de>...
    > Jean Stax wrote:
    > > Hi !
    > >
    > > I tried to understand when the explicit attribute in copy constructor
    > > prevents from
    > > me to create a new object. Bellow is the sample code.
    > > While the two first cases really generate a compilation error, the
    > > third (mc2 = Foo1();) compiles and runs without any problem. I am
    > > wondering why the:
    > >

    >
    > In neither case where you have an error is the instance of
    > MyClass a const, and you have said that the compiler is not
    > allowed to implicitely cast from non-const to const when
    > using the copy-ctor.
     
    Jean Stax, Nov 23, 2003
    #3
  4. "Jean Stax" <> wrote in message
    news:...
    > Hi !
    >
    > I tried to understand when the explicit attribute in copy constructor
    > prevents from
    > me to create a new object. Bellow is the sample code.
    > While the two first cases really generate a compilation error, the
    > third (mc2 = Foo1();) compiles and runs without any problem. I am
    > wondering why the:
    >
    > MyClass Foo1()
    > {
    > MyClass mc(1);
    > return mc;
    > }
    >
    > isn't forbidden, when MyClass copy constructor is defined as explicit.
    >
    > Thanks.
    >
    >
    > class MyClass
    > {
    > public:
    > MyClass(int nVal) : m_nVal1(nVal){}
    > MyClass() : m_nVal1(0){}
    > explicit MyClass(const MyClass& copy)
    > {
    > //...
    > }
    > private:
    > int m_nVal1;
    > };
    >
    > void Foo(MyClass cc)
    > {
    > }
    >
    > MyClass Foo1()
    > {
    > MyClass mc(1);
    > return mc;
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > MyClass mc(1);
    > MyClass mc1 = mc;
    > //error C2440: 'initializing' : cannot convert from 'class MyClass' to
    > //class MyClass' No copy constructor available for class 'MyClass'
    >
    > Foo(mc);
    > //error C2664: 'Foo' : cannot convert parameter 1 from 'class MyClass'
    > //to 'class MyClass'No copy constructor available for class 'MyClass'
    >
    > MyClass mc2;
    > mc2 = Foo1();
    >
    >
    > return 0;
    > }


    Looking at my (now ancient) 2nd edition of Stroustrup, the first two require
    implicit conversions and should not compile in any case. As for the third,
    I will step out on a limb and say:

    The first two both require the compiler to duplicate an existing object.
    Foo1(), on the other hand, produces a temporary object. In this case, the
    compiler could allow mc2 to be the actual object created by Foo1() (even
    though two lines of code are involved) in which case it would not be
    necessary to copy anything.

    Tom
     
    Thomas Wintschel, Nov 23, 2003
    #4
  5. Jean Stax

    lilburne Guest

    Jean Stax wrote:

    > Unfortunatelly, I can't see how your point makes the difference:
    >


    You are probably right.

    GCC rejects three usages the return from Foo1(), and the two
    instances you outlined.

    The nearest I can ascertain is that the explicit specifier
    on a copy ctor makes the ctor unusable in contexts where
    'copy initialization' is performed, e.g., return by value
    and pass by value. in addition to the MyClass mc1 = mc case.
     
    lilburne, Nov 23, 2003
    #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:
    601
    tom_usenet
    Sep 22, 2003
  2. Christoph Bartoschek

    Explicit copy constructor

    Christoph Bartoschek, Apr 27, 2004, in forum: C++
    Replies:
    6
    Views:
    6,659
    Christoph Bartoschek
    Apr 28, 2004
  3. trying_to_learn
    Replies:
    8
    Views:
    4,731
    Victor Bazarov
    Nov 18, 2004
  4. denis

    explicit copy constructor

    denis, Jun 16, 2007, in forum: C++
    Replies:
    1
    Views:
    340
    Ian Collins
    Jun 16, 2007
  5. cinsk
    Replies:
    35
    Views:
    2,611
    James Kanze
    Oct 11, 2010
Loading...

Share This Page