Refer to the object using class function
Hi, I was curious whether or not I could do something like this:
1 2 3
|
void some_class::foo() {
self.var = 1;
}
|
instead of doing this:
1 2 3
|
void some_class::foo(some_class& obj) {
obj.var = 1;
}
|
If var is a member of some_class you don't need to put anything in front.
1 2 3
|
void some_class::foo() {
var = 1;
}
|
If you have a local variable with the same name you can use the this keyword to disambiguate.
1 2 3 4
|
void some_class::foo() {
std::string var = "What am I doing here?";
this->var = 1;
}
|
Note that this is a pointer so that is why an arrow (
->) was used instead of a dot (
.).
Last edited on
Exactly what I wanted! Thanks :)
Topic archived. No new replies allowed.