In the earlier versions of C++ you can modify the this pointer e.g. something like that:
Class1::Class1 ()
{
if (this ==NULL) // Called with "new"
//
else // Object not created dynamic
//
}
The latest versions of C++ do not allow to modify this pointer so your line
this = new Class(rhs);
is illegal e.g. cannot be modified.
The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
"This" pointer is passed as a hidden argument to the function
For example:
Class c1;
Class c2;
c1.Swap(c2);
is translated in this way:
Swap(&c1,c2);
Now, do not confuse this pointer with a NULL pointer to an object. Here is an example:
class A
{
public:
A(){};
void WhoIam()
{
cout<<" I am A class"<<endl;
}
//
}
void main (void)
{
A *p = NULL;
p->WhoIam();
}
This program will work fine
Generally the code should be :
if (p)
p->WhoIam();
but looking at the code of the WhoIam() method there is no need to check the pointer to the object.
In this case not only p is NULL but also "this" hidden pointer passed is also NULL and you cannot modify it directly but this will be modified if you point to another object of A type:
void main (void)
{
A a;
A *p = NULL; // null pointer e.g. do not point anywhere
p->WhoIam(); // "this" is NULL
p=&a;
p->WhoIam(); // "this" is modified e.g. "this" is not NULL
}
-obislavu-