Reputation: 1853
I have read copy constructor is called when "Copy an object to return it from a function."
So I understand the copy constructor is called whenever we return a object is my understanding correct?
If yes, then whenever we return an object the constructor will be called. So, If we were in a mid of program copy constructor will be called. Then values will be assigned to the data member of the class. So the existing value will be replaced?
If no, what is the meaning of the sentence?
SRC: Tutorial point
#include <iostream>
using namespace std;
class demo
{
public:
int rate;
demo(int init_rate);
demo( const demo &obj_passed);
demo display();
};
demo::demo(int init_rate)
{
cout << "\nNormal Construtor" << endl;
rate=init_rate;
}
demo::demo(const demo &obj_passed) // Copy Constructor
{
cout << "\nCopy constructor" << endl;
rate=obj_passed.rate;
}
demo demo::display()
{
demo temp(10);
temp.rate=45;
return temp; //copy constructor is not called here
}
int main( )
{
demo obj1=obj1.display();
return 0;
}
and output is
Normal Constructor
Upvotes: 1
Views: 3001
Reputation: 1
Whenever you return a local object of a function, the compiler tends to keep and redirect the local object to the place you return(so the local object won't be deleted and compiler need not to create another object to copy the detail of the local object.) After the statement finishes(;),the local object is then deleted. On the other hand,if you return a formal parameter,compiler will copy the corresponding argument(with copy constructor),then return the clone of the argument.
Upvotes: 0
Reputation: 279445
Suppose T is the type of the object, so we have a function:
T foo() {
T returnvalue;
// do some stuff to the return value
return returnvalue;
}
And some code that calls it:
T t = foo();
Then returnvalue
is an object local to the function foo
. t
is an object local to the function that calls foo
. Finally, "the return value of foo
" is a temporary object. All three objects have type T
.
Nominally, returnvalue
is copied to the temporary object, and then the temporary object is copied to t
, so the copy constructor could be called twice. That's what your reference is talking about. No values are replaced, the copy constructor is called in order to intialize one object as a duplicate of another, separate object of the same type.
However, it is not so simple as the copy constructor always being called when an object is returned. In this example the C++ implementation is permitted to optimize by a mechanism called "copy constructor elision", in which it actually uses the same location for all three objects, and simply omits the copying. So in practice for this code you could see the copy constructor called 0, 1 or 2 times.
That's for C++03. In C++11 there are circumstances (again, my example is one of them) in which the move constructor is called in preference to the copy constructor. So in C++11 the copy constructor is guaranteed not to be called if T
has a move constructor. Again, the moves are eligible for elision, so you might see 0, 1 or 2 of them.
Upvotes: 10
Reputation: 88027
You seem to be confused about the meaning of a copy constructor. A copy constructor is used to create a new object. So a copy constructor does not replace any values, it copies old values to a new object.
Upvotes: 4