hi
i have a query regarding the copy constructor. As per my understanding, explicit copy constructor needs to be implementated explicitly to avoid the problem of recursive call to the copy constructor. I am not able to understand the exact scenario where the system will go for recursive call. Following is the code in which the copy constructor will be implicitly called.
In the above code snippet, both the classes are not provided with copy constructors. When i try to construct temp2 with temp1, the compiler will provide the implementation in a bit wise copy manner. As per my understanding, when the system tries to initialize temp2.m_ct1, the recursive call should result. But when i execute the code, i did not come across any recursive mechanism and the code just worked fine.
Can anybody tell me the situation where the recursive mechanism will come in to play ? I am confused in to the way the compiler synthesizes the copy constructor.
thanks in advance.
sanjay
i have a query regarding the copy constructor. As per my understanding, explicit copy constructor needs to be implementated explicitly to avoid the problem of recursive call to the copy constructor. I am not able to understand the exact scenario where the system will go for recursive call. Following is the code in which the copy constructor will be implicitly called.
Code:
class CTest {
private:
int m_data1;
};
class CMyClass {
private:
int m_data;
CTest m_ct1; // class object.
public:
~CMyClass() {}
};
int main(void) {
CMyClass temp1;
CMyClass temp2 = temp1;
}
In the above code snippet, both the classes are not provided with copy constructors. When i try to construct temp2 with temp1, the compiler will provide the implementation in a bit wise copy manner. As per my understanding, when the system tries to initialize temp2.m_ct1, the recursive call should result. But when i execute the code, i did not come across any recursive mechanism and the code just worked fine.
Can anybody tell me the situation where the recursive mechanism will come in to play ? I am confused in to the way the compiler synthesizes the copy constructor.
thanks in advance.
sanjay