this pointer question

Status
Not open for further replies.

Lucifre

Full Member level 2
Joined
Jul 5, 2005
Messages
125
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,296
Location
Detroit MI
Activity points
2,389
What does
Code:
return *this;
mean? code sniplets to explain would be apreciated
 

"this" pointer will point to the currently referring class object. So "*this" will be the data value of the class object. Hence
Code:
.............
..............

Class1 Class1::func1() {
    return *this;
}

................
................
Class1 var1, var2;
var2 = var1.func1();

will always set "var2" by the value of the class object with which the member function was called (var1).
This may be overrided by using an assignment operator (Class1:perator=()). But if the return type was by refernce (ie: Class1& func1()) "Class1 &var2 = var1.func1();" will set var2 as a reference to var1. Also this may have a different behaviour if the return (by value) type was set to initialize a class object.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…