C++: Why in a copy constructor is an object collected in a reference to an object as shown below?
Why in a copy constructor is an object collected in a reference to an object as shown below?
#include <iostream.h>
class emp
{
public :
emp( )
{
}
emp ( emp& )
{
cout << "copy" ;
}
} ;
void main( )
{
emp e ;
emp e1 = e ;
}
A copy constructor is called when an object is created and initialised at the same time. It is also called when object is passed to a function. So, If we pass the object to a copy constructor, copy constructor would get called resursively. Thus it will become stuck in an infinite loop.




Comments
Log in or create a user account to comment.