Thursday, July 30, 2009

C++ question?

Please tell me in C++ how does return *this; relates to returning a reference in assignment operator overloading





I am confused because this is a pointer to the current object and the return value of the function is ClassName %26amp; which is a reference. so how can ClassName%26amp; be the return type when we are returning using return *this; statement.





sample code:





MyClass%26amp; MyClass::operator=(const MyClass %26amp;rhs) {


... // Do the assignment operation!





return *this; // Return a reference to myself.


}

C++ question?
well a reference is like a pointer whose address you can't set so *this returns a MyClass Object and c++ can automatically make a MyClass reference from an objec.t

survey for money

No comments:

Post a Comment