views:

3956

answers:

4

I understand copy constructor is called on three instances

  1. When instantiating one object and initializing it with values from another object (as in the example above).
  2. When passing an object by value.

3. When an object is returned from a function by value.

I have question with no.3 if copy constructor is called when an object value is returned, shouldn't it create problems if object is declared locally in the function.

i mean the copy constructor is a deep copy one and takes reference of an object as parameter

A: 

No, it calls it before the locals are destroyed. You can test this with an object that logs destruction and copy construction, or by looking at the generated assembly code.

Lou Franco
but what if the copy constructor takes reference as a parameter(deep copy), in that case it would not copy the whole object, right?
Kazoom
Depends on the copy constructor. The default one does a member-wise copy construct. Normally, you should implement your copy constructor to not rely on the lifetime of the object that was passed in. You don't have to deep copy, but you need to make sure shared stuff stays around.
Lou Franco
+7  A: 

It's called exactly to avoid problems. A new object serving as result is initialized from the locally-defined object, then the locally defined object is destroyed.

In case of deep-copy user-defined constructor it's all the same. First storage is allocated for the object that will serve as result, then the copy constructor is called. It uses the passed reference to access the locally-defined object and copy what's necessary to the new object.

sharptooth
+5  A: 

The copy is done before the called function exits, and copies the then-existing local variable into the return value.

The called function has access to the memory the return value will occupy, even though that memory is not "in scope" when the copy is being made, it's still available.

unwind
Kazoom
If the function is declared as returning a reference returning a reference to a local variable is a big no-no to do. You will return a reference to an object that will be discarded on function return. Using this reference will likely cause problem, crushes included.
sharptooth
yes i understand that, so when a copy constructor which is taking reference as parameter is being called when returning the value, what would happen? shouldn't it crash too?
Kazoom
What's the problem? The function is about to return. The copy constructor is invoked. It takes a reference to the local variable. It uses this reference to copy everything into the new object that will be used as the return value.
sharptooth
Added it to my answer.
sharptooth
+1  A: 

According to an answer to my question, the copy constructor may be called even twice: once to copy a local object onto the return 'object', and once to copy the return object onto the variable it was assigned to.

However, it needn't be! The compiler can optimize both copy constructions away.

xtofl