Let's take a class 'A' that has a data member - int *ptr
- pointing to a dynamically allocated memory on heap. This class must have a defined destructor to deallocate memory when its objects are destroyed. And, according to Rule of three, it should also have a copy constructor, and an overloaded copy assignment operator too. Here is the declaration of 'A':
class A {
public:
A(); // constructor
A(const A&); // copy constructor
A& operator=(const A&); // assignment operator
~A(); // destructor
// ... more methods ...
private:
int* ptr;
};
We have implemented the copy assignment operator of 'A':
A& operator=(const A& rhs) {
ptr = rhs.ptr;
return *this;
}
Above overloaded assignment operator function has some major problems. Tell us what are all those problems by selecting appropriate choices below: