The this Pointer and Encapsulation
Use the this pointer and enforce encapsulation with private and public access.
The this Pointer and Encapsulation is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is this?
Inside a non-static member function, this is an implicit pointer to the current object. Use it to disambiguate or to return references.
class Counter {
int n_ = 0;
public:
Counter& add(int x) {
n_ += x; // implicit this
return *this; // chain calls
}
};Implicit Access
You usually do not need to write this->. Member names are looked up automatically — this->n_ and n_ are equivalent.
When this-> Is Required
Use this-> when a parameter shadows a member, or when template lookup needs help finding inherited members.
class Person {
std::string name_;
public:
void setName(const std::string& name) {
this->name_ = name; // or name_ = name;
}
};Returning *this for Chaining
Return *this to support fluent interfaces.
class StringBuilder {
std::string s_;
public:
StringBuilder& append(const std::string& part) {
s_ += part;
return *this;
}
};
StringBuilder sb;
sb.append("Hello, ").append("World!");this in Const Methods
Inside a const method, this has type const ClassName*. You cannot modify members through it.
Encapsulation: The Big Idea
Encapsulation means hiding internal state and exposing only safe operations. Make data private; expose intentional public methods.
class Stack {
std::vector<int> data_; // hidden
public:
void push(int v) { data_.push_back(v); }
int pop() { int v = data_.back(); data_.pop_back(); return v; }
bool empty() const { return data_.empty(); }
};Why Encapsulate?
Encapsulation lets you:
- Change the internal representation without breaking callers
- Enforce invariants in one place
- Validate inputs at the boundary
- Reason about object state locally
Public Interface = Contract
The public interface is a promise to callers. Keep it minimal, intentional, and stable. Make implementation details private.
Getters and Setters
Common pattern but easy to overuse. Avoid mirroring every member with get/set — that defeats encapsulation. Expose operations, not fields.
Const Correctness Again
Mark every read-only method const. Callers know which methods are safe to call on const instances and the compiler enforces it.
Friend Functions and Classes
The friend keyword grants another function or class access to private members. Use sparingly — it tightens coupling.
class Account {
double balance_;
friend void transfer(Account& a, Account& b, double amount);
};Quick Check
What is the type of this inside a method declared void f() const of class Foo?
Recap
this points to the current object inside non-static methods. Use it for chaining (return *this) and disambiguation. Encapsulate state with private data and a small, intentional public interface.
Frequently asked questions
Is the “The this Pointer and Encapsulation” lesson free?
Yes — the full text of “The this Pointer and Encapsulation” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “The this Pointer and Encapsulation”?
Use the this pointer and enforce encapsulation with private and public access. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The this Pointer and Encapsulation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- struct vs class When to Use Each
- Member Variables and Methods
- Constructors and Member Initializer Lists
- The this Pointer and Encapsulation