[   ^ to index...   |   <-- previous   |   next -->   ]

Classes and dynamic memory

Classes that uses dynamically managed memory introduce complexity. There are 2 major problems that an object that uses dynamic memory must deal with:

  1. Copying: whenever an object is initialized using another object, or assigned to another object, a deep copy must be made instead of a shallow copy.


  2. Destruction: whenever an object is deallocated, including during an assignment operation (why?), the dynamic memory must be deallocated as well.
class Vector { public: Vector(); Vector(Vector& other); Vector& operator=(Vector& other); ~Vector(); // ... }; foo(Vector a, Vector& b, Vector* c); // arbitrary function

Assuming this Vector dynamically resizes itself (somehow), and all methods are correct, what do each of the following do? Which lines invoke the no-argument constructor, copy constructor, destructor, and overloaded assignment operator? Which ones are legal and illegal? Why? Assume the statements are executed in order.


Last modified: Wed Jul 19 18:28:43 PDT 2000