You may not understand the this
pointer.
C++ requires every function to receive all data using arguments.
If you look at your class member functions you will see that no member function has an argument for the class itself. Also yu will see that no member function has an argument for which object to work with.
What happens is that the address of the object is passed in by the compiler as an invisible first argument.
- class MyClass
-
{
-
private:
-
int data;
-
public:
-
void Add(int x);
-
};
-
void MyClass::Add(int x)
-
{
-
data = x;
-
}
You can't tell what the "data" is. It might be a global variable ot it might be inside a MyClass object but you can't tell which one. The correct way to write this meber function is:
- void MyClass::Add(int x)
-
{
-
this->data = x;
-
}
This says that data is inside the MyClass object pointed at by
this. That makes
this a MyClass*. You don't see the argument for a MyClass* in the function but it is there as an invisible first argument.
So if
this is a MyClass*, then *this must be a MyClass variable.
Therefore,
cout << *this is a call to an operator<< function that has an ostream and a MyClass argument.
Whereas
cout << this is a call to an operator<< function that has an ostream and a MyClass* argument.
I hope this helps.