Skip to main content

What is a copy constructor ?What is the need for it ? What is the Shallow and deep copy ? When it gets called ? Write the signature ? What is the diff

Copy constructor invoked in following threee situations.

1. When a new object is created using existing object.

Ex. MyClass obj1;

MyClass obj2 = obj1;

2. When any object is passed by value.

Ex. void function( MyClass obj1) ;

MyClass obj2;

Function( obj2);

3. When any object returned by value.

Ex. MyClass function( ) {

MyClass obj2;

Return obj2;

}

Default Copy constructor performs the member by member copy or shallow copy

1. Shallow Copy : If user doesn't provides the Copy constructor , compiler provides one by default to the class. User needs to provide a Copy constructor for a class, if the class is having any memory allocations to member pointers.

Ex, class MyClass {

int *m_pData ;

};

MyClass ( Const MyClass & objectSource) {

m_pData = objectSource.m_pData;

}

Problems Caused: If there are any memory allocations and user is not providing the user defined Copy constructor , default Copy constructor will be called. It will do a member by member shallow copy. it means it will assign the same memory allocated inside one object to the other object. Both object will point to the same memory location. If one object modify values for its own use, same values will be available to another object. This turned into using wrong values for another object.

If such objects will going to be destroyed. For first object the memory will be freed and second object will again try to free the already freed memory by object one. This may corrupt the hip.


To avoid above scenario, User defined Copy constructor must be provided which provides the Deep Copy;

2. Deep Copy :

In deep copy first memory is allocated and then member values are copied.

MyClass ( Const MyClass & objectSource) {

m_pData = new int;

*m_pData = *(objectSource.m_pData);

}

Passng parameter(object) by value instead of refference.

If object is passed by value, again copy constructor will be called which will call again copy constructor. Thus the calling will be recursive and control will come back only after stack overflow.

Thus parameter to Copy constructor must be refference.

difference between copy constructor and overloaded assignment operator

Copy constructor first creates a new object and then copies the values from already created object to the new one.

Assignment operator copies values from one existing (already created) obect to another existing (already created) obect.


Comments

Popular posts from this blog

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.

• What are the advantage and disadvantage of using exception handling?

Disadvantage is a slight overhead imposed by implementing of exception handling mechanism. Advantage is "bullet-proof" program. With exception handling you have a mechanism which guarantee you control over program behavior despite the errors that might be in your program. With try-catch block you control not even given block of the program, but also all underlying function calls.