Saturday, December 10, 2005

c++ Copy Constructor Conventions

A couple of very simple to follow rules for copy constructors.
Case A: An object has no dynamically allocated pointers. Let the default copy constructor do the work and don't bother writing one.

Case B: If said object does contain dynamic pointers, then you'll need to write your own copy constructor as follows.
MyObject(const MyObject& o) {
// Do some stuff for a true deep copy
// Make new pointers and stuff :)
}

0 Comments:

Post a Comment

<< Home