In C++, the assignment operator (=) is just a function. If you write obj = obj; (assigning an object to itself), a naive implementation might destroy the object's data before trying to copy it, leading to corruption or crashes. This is especially dangerous with resources like buffers or file handles.
To prevent this, every assignment operator must first check: "Is the source address the same as my address?"
Your task is to implement a class Buffer.
int id.Buffer(int i) : id(i) {}.if (this == &other).Self-Assignment Detected and return *this immediately without changing anything.id from other, print Assignment Complete, and return *this.void log(): Print Buffer ID: <id>.Program Flow:
N.N times.cmd.cmd is "ASSIGN":id1 and id2.Buffer objects (b1, b2) with these IDs.b1 = b2;.b1.log().cmd is "SELF":id.Buffer b1(id).b1 = b1; (Self-Assignment).b1.log().Input Format:
N.N lines: String cmd, followed by values.Output Format:
Assignment Complete followed by Buffer ID: <new_id>Self-Assignment Detected followed by Buffer ID: <original_id>Example:
Example 1
Input:
2
ASSIGN 10 20
SELF 99Output:
Assignment Complete
Buffer ID: 20
Self-Assignment Detected
Buffer ID: 99Constraints:
if (this == &other) inside operator=.*this to allow chaining.
Input
2 ASSIGN 10 20 SELF 99
Expected Output
Assignment Complete Buffer ID: 20 Self-Assignment Detected Buffer ID: 99