113. Operator Overloading-I

Question.4

A developer implements the copy assignment operator for a buffer class:

class Buffer {
   uint8_t* data; int size;
public:
   Buffer& operator=(const Buffer& other) {
       delete[] data;
       size = other.size;
       data = new uint8_t[size];
       memcpy(data, other.data, size);
       return *this;
   }
};

Buffer buf;
buf = buf;  // Self-assignment!

What happens on buf = buf?

Need Help? Refer to the Quick Guide below

Select Answer