previous | start | next

Copying

Since C++ allows variables declared to hold either class values or pointers (references) to class values, one has write classes to be prepared to handle the situations where C++ automatically makes copies of class values and automatically destroys class values:

You may need to define:

a. Copy constructor    Used for passing class objects by value
b. operator=           (assignment operator)
c. destructor          

When do you need to use the "rule of 3" and define these members? Typically when the class has pointer members which the class itself has allocated the storage on the heap using new and is responsible for this extra storage. The extra part on the heap is not automatically copied by the default copy constructor, default assignment operator. The extra storage on the heap is also not automatically deleted when the object containing the pointer member is deallocated.



previous | start | next