You are not logged in.

#1 2013-02-13 10:43:56

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 673

Copy constructor and temporary objects in C++

I'd need some clarification on how constructor works when the argument is  a temporary object. I defined a dummy MyVect class with operator+ and copy constructor:

class MyVect {
public:
  MyVect() {
    cout << "Constructor" << endl;
  }

  MyVect(const MyVect &vect){
    cout << "Copy Constructor" << endl;
  }

  ~MyVect() {
    cout << "Destructor" << endl;
  }

  MyVect operator+(const MyVect &v) const {
    cout << "operator+" << endl;
    MyVect result
    return result;
  }

}

Then I test it with this very simple code pattern:

MyVect v1;
MyVect v2;
MyVect v3(v1 + v2);

The output is:

Constructor
Constructor
operator+
Constructor
Destructor
Destructor
Destructor

The first two constructor calls are for v1 and v2, then operator+ for  v1+v2 is called, and  inside it there is a constructor call for result object. But then no copy constructor (nor constructor) for v3 is called. In my limited understanding of C++, I guessed that once the temporary object resulting from v1+v2 has been created, then it would be the argument of the copy constructor call. Instead it looks like the compiler transforms the temporary into v3, since v3 is correctly initialized as the sum of v1 and v2 (I tried with a more complicated version of MyVect to verify this). This behavior reminds me of the move semantics introduced with C++11, but I'm not using the -std=c++11 flag (and in fact if I try to define a move constructor the compilation fails).
Can anyone please help me to understand what's happening under the hood? Thanks.

Last edited by snack (2013-02-13 10:44:28)

Offline

#2 2013-02-13 13:56:06

Lord Bo
Member
Registered: 2012-11-11
Posts: 151

Re: Copy constructor and temporary objects in C++

wikipedia wrote:

The following cases may result in a call to a copy constructor:
When an object is returned by value
When an object is passed (to a function) by value as an argument
When an object is thrown
When an object is caught
When an object is placed in a brace-enclosed initializer list
These cases are collectively called copy-initialization and are equivalent to:[2] T x = a;
It is however, not guaranteed that a copy constructor will be called in these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example being the return value optimization (sometimes referred to as RVO).

Last edited by Lord Bo (2013-02-13 13:58:47)

Offline

#3 2013-02-13 17:54:43

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 673

Re: Copy constructor and temporary objects in C++

So basically RVO is something compiler-dependent and is not guaranteed. While with move semantics one can force objects to be moved instead of being copied (and something more, AFAIK).
Thanks Lord Bo.

Offline

#4 2013-02-13 19:44:35

Lord Bo
Member
Registered: 2012-11-11
Posts: 151

Re: Copy constructor and temporary objects in C++

No Problem. This behaviour of constructors/destructors can be very confusing, I know that.
edit: IMHO it is a big problem, that the behaviour of such things may vary and that the language specification of c++ is just to big to be known by everyone by heart. Unfortunatly I couldn't say, how one should make it better.

Last edited by Lord Bo (2013-02-13 19:50:22)

Offline

#5 2013-02-13 22:19:29

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 673

Re: Copy constructor and temporary objects in C++

I think that one can do nothing but be aware of it and live with it. I'm glad I had the opportunity to learn it, in fact I was preparing some slides for an undergraduate C++ course I'm preparing when I ran into this issue. Good thing I've been able to understand that my belief was wrong before loosing all my students' faith wink

Offline

Board footer

Powered by FluxBB