PFA the link . I have defined the operator() outside the class . What is wrong in this ?
Copy Assignment Operator code not working
When you perform operator overloading with two operands as you have done in your code, but taking only one argument in the function definition, then the compiler implicitly takes the other operand( always the left or first argument).
Let’s understand this in reference to your code.
At the time of function call i.e.
a=b;
Object “a” will act as a first argument and “b” will be the second argument
But your function definition contains only one parameter i.e. X
So, reference of b will be pass as X
Thus, X.a will point to the member variable of object b.
And this->a will refer to the member variable of object a.
In your code you are assigning an unintialised value to the the member variable of object a.
X.a=this->a;
In simple sense:
b.a=a.a;
This can cause an error
The correct way is
this->a=X.a;
Or
a=X.a;
Try this, your code will run.
If you still have any doubts, feel free to ask.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.