← Back to C++ Course | Chapter 8: OOP Core | Lesson 8 of 14

C++ this Pointer

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

cpp
#include <iostream>

class Box {
	int size = 5;
public:
	void show() { std::cout << this->size << std::endl; }
};

int main() {
	Box b;
	b.show();
	return 0;
}

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

cpp
#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;
}

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'

cpp
#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;
}

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

cpp
#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;
}

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'

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.