C++ this Pointer
In this page:
Understanding the 'this' Pointer
Every non-static member function in C++ implicitly receives a hidden pointer to the object it was called on, accessible inside the function as this. It's how a method knows which specific object's data to operate on when many objects of the same class exist.
Example: Understanding the 'this' Pointer
#include <iostream>
class Box {
int size = 5;
public:
void show() { std::cout << this->size << std::endl; }
};
int main() {
Box b;
b.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Resolving Variable Name Conflicts
When a constructor or method parameter happens to share a name with a member variable, the parameter name would normally shadow the member inside that scope. Writing 'this->memberName' explicitly tells the compiler you mean the class's member variable, not the local parameter.
Example: Resolving Variable Name Conflicts
#include <iostream>
class Point {
int x;
public:
Point(int x) {
this->x = x; // "this->x" is the member, "x" is the parameter
}
void show() { std::cout << x << std::endl; }
};
int main() {
Point p(7);
p.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Method Chaining with 'this'
You can return '*this' as a reference from a member function, which lets the caller chain multiple method calls together on the same object in a single expression — the pattern behind fluent interfaces like obj.setX(1).setY(2).setZ(3).
Example: Method Chaining with 'this'
#include <iostream>
class Builder {
int value = 0;
public:
Builder &add(int n) { value += n; return *this; }
int get() { return value; }
};
int main() {
Builder b;
std::cout << b.add(1).add(2).add(3).get() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing 'this' as an Argument
You can pass the this pointer as an argument to a free function outside the class, letting that external function operate directly on the calling object's data as if it were a member function itself, without needing full member-function access.
Example: Passing 'this' as an Argument
#include <iostream>
class Box;
void describe(Box *b);
class Box {
public:
int size = 5;
void show() { describe(this); }
};
void describe(Box *b) {
std::cout << "Box size: " << b->size << std::endl;
}
int main() {
Box box;
box.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Functions and 'this'
Inside a const member function, the type of this effectively becomes a pointer to a constant object, which means the compiler will reject any attempt inside that function to modify a member variable — this is how const methods enforce their read-only promise.
Example: Constant Functions and 'this'
#include <iostream>
class Box {
int size = 5;
public:
int getSize() const { return size; } // "this" is a pointer to a const object here
};
int main() {
const Box b;
std::cout << b.getSize() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: