Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use of "this" pointer . . .

Status
Not open for further replies.

Wolfie7873

Technical User
Jan 15, 2004
94
US
Assuming a valid copy constructor as follows:

Class::Class(const Class& other)
{
myData=other.myData;
}

can I have this for my overloaded assignment?

Class&
Class::eek:perator=(const Class& rhs)
{
if (this != &rhs) {
this = new Class(rhs);
}
return *this;
}

I didn't know if I'm allowed to make direct assignments to the "this" pointer.

Thanks,
Eddie
 
No. I dont think thats legal. However you are sort of on the right lines... Behold....

Class& Class::eek:perator =( const Class& rhs)
{
Class temp(rhs);
Swap(temp);
return *this;
}

void Swap(Class& a)throw()
{
std::swap( Data_ , a.Data_ );
}

Now Swap is a private member function that calls std::swap on each data member of the class. Simple and effective and exception safe strongly.
 
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<<&quot; I am A class&quot;<<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 &quot;this&quot; 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(); // &quot;this&quot; is NULL
p=&a;
p->WhoIam(); // &quot;this&quot; is modified e.g. &quot;this&quot; is not NULL
}

-obislavu-
 
Ok, what about this:

Class&
Class::eek:perator=(const Class& rhs)
{
if (this != rhs) { // i use this to avoid unnessary ops
Class* temp = new Class(rhs);
return *temp;
}
else
return *this;
}

That should check to see if assignment is necessary - if it isn't, it gives you back what you started with, otherwise you construct a new pointer and allocate the data members appropriately and return the created reference.
Am I going to have a problem with the newly created object falling out of scope?

 
That is not how to write an assignment operator. I have shown you the correct way. A less exception safe way would be this....

Class& operator =(const Class& rhs)
{
if (this = &rhs)
{
// clean up old members
// such as if you had a member
// initialised with new then here
// you would call delete on it

// now here you allocate new resources
// that are the same as rhs's members
}
return *this;
}

I believe this is what you are trying to do but you are failing in your understanding of exactly what the assignment operator must do. Prefer the version I have showed you already. A check for self-assignment is not necessary in that version and all the real work is done with just two functions, the copy constructor and Swap. Its elegant. You make a local object then swap the guts of your object with the guts of the local object. Your object is now effectively rhs and the local temp goes out of scope and its destructor cleans up the guts of your old object.Drill this idiom into your head, it will pretty nearly always be the best way of writing an assignment operator.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top