Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query on copy constructor.

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
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.

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
 
It's not a problem of recursive call, it's another problem of a class object content copy semantics. No such problem in the snippet above classes because of a simple copy member by member (defauly copy constructor behaviour) is OK fo them...
But, for example:
Code:
class Class
{
   Class(); // allocate buffer
  ~Class(); // deallocate buffer
...
char* dynbufptr; // 
};
In that case you can't copy dynbufptr into a new object; you must allocate a new buffer for it. So you must present your own copy constructor for Class (and assignment operator too, with different semantics: it must deallocate left side object buffer) for this class.
Why recursion?.. It's semantics issue...
 
Hi AkrM
Thanks for the reply. In the code snipet you mentioned, it is the programming need for the code to carefully allocate and deallocate the memory. Even in case, the proper memory management is not handled in the code, the member wise copy will happen . Although there may be chances of memory corruption due to more than one objects being using the same memory [heap] area.
I will try to put my query in other words as .... i wanted to understand the situation where in the copy constructor will go in to recursive call if not explicitly implemented.when the compiler tries to synthesize or provide the default copy constructor, what will be the prototyping for the same. When i say
Code:
  class MyClass {
    public:
       MyClass(const MyClass&);
    // Some code here
  };
In the above code, i provide the copy constructor with argument as reference to MyClass type. If i have the copy constructor as follows.

Code:
   class MyClass {
       public:
          MyClass(MyClass);
   };

The object creation with copy constructor will go for recursive call as per my understanding. But when i try to run the code with above copy constructor, it worked fine. I mean there was no recursive calls. This situation is not clear to me.

Thanks
sanjay
 
MyClass(MyClass) is not a copy constructor.
Copy constructor (generated or user defined) signature is MyClass(const MyClass&) only. No (never;) recursion at all...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top