Thursday, July 9, 2009

In C++ when should you define a copy constructor?

If you're class contains any dynamically allocated attributes then you DEFINITELY want to define a copy constructor, along with an assignment operator and a destructor. Here's why.





If you don't define a copy constructor, the C++ compiler will provide a default one for you. However, the default copy constructor only does a shallow copy. That means that it will only copy the "value" of the attributes in your class. If the class contains pointers of any form, then the copied object and the source object will have attributes pointing to the same memory.





Consider the following...





You have a Class, which contains a dynamic attribute, and you don't define a copy constructor. Watch what happens.





Class x; // The dynamic attribute is declared in the constructor.





{ // Start a new block


Class y = x; // The copy constructor is called, but


// y's dynamic attribute is pointing to x's


// as well. It doesn't have its own copy.


} // y goes out of scope, and its destructor is called,


// which deletes the dynamic attribute.





// OOPS. x's dynamic attribute has become a dangling


// reference. It will core dump with a segmentation


// violation when x's dynamic attribute is accessed.





For similar reasons, the assignment operator (operator=) should also be defined when a class contains pointer attributes. (Don't forget to release the attributes of the object being copied over!!!)





If you don't intend for your users to access these methods, and you don't want to take the trouble to define them, then make them private.





Read Scott Meyer's first Effective C++ book. He spends several chapters on this topic. It's very easy to leak memory or create dangling references if you're not careful. I'm constantly checking my code for problems along these lines.

In C++ when should you define a copy constructor?
If the member by member copying of the default copy constructor is not appropriate for instances of your class then you either need to define a copy constructor or declare a non public copy constructor.
Reply:Suppose ur class is having some member variables, say, 5.


In this 5 members, let 3 of them hold data and the other 2 to manage some other behaviour of the class.


Then u want to assign one obj to other.


eg:-


CMyClass a,b;


// some operations in these objs


a = b;





Suppose u need to keep the 3 (NOTE: only 3) data members of object B to object A.


In such a situation, u must define a copy constructor.





Copy constuctor is a constructor that accepts its own object as argument. Refer some C++ books for more details..





Hope this will help u..
Reply:A Copy constructor is defined when u need to instantiate a new object using an instantiated object.





Eg:


MyClass obj1("Blah");





MyClass obj2(obj1);





For more on it check these links:





http://cplus.about.com/od/beginnerctutor...


http://www.fredosaurus.com/notes-cpp/oop...


http://publib.boulder.ibm.com/infocenter...

tarot cards

No comments:

Post a Comment