Classes that uses dynamically managed memory introduce complexity. There are 2 major problems that an object that uses dynamic memory must deal with:
- 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.
- 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.
- Vector v();
- Vector* v2 = new Vector();
- Vector v3 = v;
- delete v2;
- v2 = &v;
- Vector v4 = v;
- v3 = *v2;
- Vector v5(v3);
- Vector* v6 = new Vector(*v2);
- foo(v4, v5, v6);