THIS has two meanings at the title , The pointer and this source code.
//assign2.cpp
// returns contents of the this pointer
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class alpha
{
private:
int data;
public:
alpha() { }
alpha(int d) { data = d; }
void display()
{ cout << data; }
alpha& operator = (alpha& a)
{
data = a.data;
cout << "\nAssignment operator invoked";
return *this;
}
what i suppose is that.. a3 = a2 = a1 starts with a2 = a1 , so the Overloaded operator takes the member a1.data and and gives its value to a2.data(or just data because a2 was the caller or wasn't it??one of the main questions baffling me) and After the value was assigned to a2.data the this pointer returns a2 object(because it was the caller?or?) so the program continues with the statement a3 = a2 (this happens because of THIS instead it wouldn't return a2 object and it would end with a3= "I don't know what" or?)
so a3 = a2 calls the overloaded = and gives a3.data the value of a2.data and then again comes the this pointer which again returns the a3 object which was the caller(or wasn't it?) which doesn't do anything. so we call display for each and see that all have same value.
works the code Exactly the way I described it, or am I totally wrong at it.
what i suppose is that.. a3 = a2 = a1 starts with a2 = a1
yes - assignment associates right to left - so the one on the right is done first.
so the Overloaded operator takes the member a1.data and and gives its value to a2.data(or just data because a2 was the caller or wasn't it??
The assignment operator is a binary operator - it takes tow operands. In binary operations it is the object on the left of the operator that does the work. The object on the right side of the operator is passed as the function parameter.
So the assignnment operator is called with the this pointer pointing to a2 and a2 copies the data from a1.
one of the main questions baffling me) and After the value was assigned to a2.data the this pointer returns a2 object(because it was the caller?or?) so the program continues with the statement a3 = a2 (this happens because of THIS instead it wouldn't return a2 object and it would end with a3= "I don't know what" or?)
so a3 = a2 calls the overloaded = and gives a3.data the value of a2.data and then again comes the this pointer which again returns the a3 object which was the caller(or wasn't it?) which doesn't do anything. so we call display for each and see that all have same value.
Pretty much - Because the object on the left is the one with the this pointer - so return *this will return a reference to a2, then a3=a2 will return a reference to a3.
With regard to the function signature - it is best to do:
1 2 3 4 5 6
alpha& operator = (const alpha& a) //notice the const
{
data = a.data;
cout << "\nAssignment operator invoked";
return *this;
}
This will avoid a possible compiler error when attempting to do assignments when temporary objects are involved.