Copy constructor doesn't get called when initialized by functionreturning object of same class

Discussion in 'C++' started by abhash, Jun 12, 2008.

  1. abhash

    abhash Guest

    I am bit puzzled at the following piece of code I tried:

    ----------------------------------------------------------------------------------
    #include <iostream>
    using namespace std;

    class Test {
    public:
    Test() { cout<<"Cons\n";}
    Test(Test& a) { cout<<"Copy cons\n";}
    };

    Test fun()
    {
    return Test();
    }

    int main()
    {
    cout<<"First way of initialization\n";
    Test t1;
    Test t2 = t1;

    cout<<"\nSecond way of initialization\n";
    Test t3 = fun();
    return 0;
    }

    OUTPUT (when compiled on CC compiler) :

    First way of initialization
    Cons
    Copy cons

    Second way of initialization
    Cons
    ------------------------------------------------------------------------------------

    I am intrigued why second initialization does call copy constructor ?
    Aren't we passing the temporary object returned by fun() call to the
    t3 for copying ?
    abhash, Jun 12, 2008
    #1
    1. Advertising

  2. abhash

    dizzy Guest

    Re: Copy constructor doesn't get called when initialized by function returning object of same class

    abhash wrote:

    > I am bit puzzled at the following piece of code I tried:
    >
    > ------------------------------------------------------------
    > #include <iostream>
    > using namespace std;
    >
    > class Test {
    > public:
    > Test() { cout<<"Cons\n";}
    > Test(Test& a) { cout<<"Copy cons\n";}


    You probably mean Test(Test const&).

    > };
    >
    > Test fun()
    > {
    > return Test();
    > }
    >
    > int main()
    > {
    > cout<<"First way of initialization\n";
    > Test t1;
    > Test t2 = t1;
    >
    > cout<<"\nSecond way of initialization\n";
    > Test t3 = fun();
    > return 0;
    > }
    >
    > OUTPUT (when compiled on CC compiler) :
    >
    > First way of initialization
    > Cons
    > Copy cons
    >
    > Second way of initialization
    > Cons


    First of all if you really had your ctor taking a reference to non const as
    you showed above then you are using a broken compiler. Your compiler should
    have complained of not finding a Test(Test const&) ctor version.

    > I am intrigued why second initialization does call copy constructor ?
    > Aren't we passing the temporary object returned by fun() call to the
    > t3 for copying ?


    Because you are one of the lucky ones using a compiler that does RVO. But
    still your compiler is broken because while the standard allows compilers
    to elude calling the copy ctor in certain situations (such as yours) for
    speed benefits, it does require that the object has a proper copy ctor AS
    IF it would have been called (even if it's not).

    Enable strict standard compliance mode in your compiler (that should get the
    compile error Im talking about), then disable any optimization (or enable
    debug mode, depends on compiler), then retry the program. You should see
    then the copy ctor getting called.

    --
    Dizzy
    dizzy, Jun 12, 2008
    #2
    1. Advertising

  3. abhash

    dizzy Guest

    Re: Copy constructor doesn't get called when initialized by function returning object of same class

    Pete Becker wrote:

    > How do you know that it didn't?
    >


    Since he said he ran the program and got unexpected output (to run the
    program means to compile it, which means what I said).

    --
    Dizzy
    dizzy, Jun 12, 2008
    #3
  4. Re: Copy constructor doesn't get called when initialized by function returning object of same class

    abhash <> writes:
    > I am intrigued why second initialization does call copy constructor ?
    > Aren't we passing the temporary object returned by fun() call to the
    > t3 for copying ?


    The compilers are allowed to optimize out this case. So the function
    fun can use the storage of t3 directly to construct a Test there,
    without any copying.

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Jun 12, 2008
    #4
  5. abhash

    dizzy Guest

    Re: Copy constructor doesn't get called when initialized by function returning object of same class

    Pete Becker wrote:

    > On 2008-06-12 12:35:48 +0200, dizzy <> said:
    >
    >> Pete Becker wrote:
    >>
    >>> How do you know that it didn't?
    >>>

    >>
    >> Since he said he ran the program and got unexpected output (to run the
    >> program means to compile it, which means what I said).

    >
    > Huh? How does compiling and running a program and getting unexpected
    > output establish that the compiler didn't give any diagnostic messages?


    Right. I was thinking about compile time error, sorry for not beeing
    explicit enough.

    --
    Dizzy
    dizzy, Jun 12, 2008
    #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. Tom Widmer
    Replies:
    1
    Views:
    494
    Victor Bazarov
    Nov 17, 2004
  2. ali
    Replies:
    4
    Views:
    558
    David Harmon
    Mar 5, 2007
  3. abhash
    Replies:
    8
    Views:
    356
    James Kanze
    Jun 14, 2008
  4. cinsk
    Replies:
    35
    Views:
    2,560
    James Kanze
    Oct 11, 2010
  5. Skybuck Flying
    Replies:
    2
    Views:
    2,516
    Alf P. Steinbach /Usenet
    Jun 21, 2011
Loading...

Share This Page