The this Pointer & const Member Functions
this
Every non-static member function has a hidden pointer this to the object it was called on.
void set_name(const std::string& name) {
this->name = name; // this->name is the member, name is the parameter
}
Usually optional — balance means this->balance — but useful when:
- a parameter has the same name as a member
- returning the object itself:
return *this; - passing the current object to another function
Method chaining (fluent interface)
Builder& add(int x) { total += x; return *this; }
b.add(5).add(10).add(20);
Return *this by reference, otherwise each call modifies a copy.
const member functions
double balance() const { return balance_; }
- Promises not to modify the object
- Inside,
thisis a pointer toconst - Only const member functions can be called on const objects (and through
const¶meters!)
void print(const Account& a) {
a.balance(); // ✅ OK, balance() is const
a.deposit(5); // ❌ error: deposit is not const
}
✅ Mark every function that doesn't modify the object const — otherwise your class can't be used through const&.
Overloading on const
int& at(int i); // for non-const objects: allows modification
const int& at(int i) const; // for const objects: read-only
mutable
A member marked mutable can be changed even in const functions — for caches or counters that don't affect the object's logical value. Use rarely.
Example
#include <iostream>
#include <string>
#include <vector>
class Report {
public:
explicit Report(std::string title) : title(std::move(title)) {}
Report& add_line(const std::string& line) { // returns *this for chaining
lines.push_back(line);
return *this;
}
Report& set_title(const std::string& title) {
this->title = title; // this-> separates member from parameter
return *this;
}
std::size_t size() const { return lines.size(); }
void print() const { // const: safe on const objects
++print_count; // allowed: print_count is mutable
std::cout << "== " << title << " ==\n";
for (const auto& l : lines) std::cout << " " << l << '\n';
}
int times_printed() const { return print_count; }
bool same_object(const Report& other) const { return this == &other; }
std::string& line(std::size_t i) { return lines.at(i); } // writable access
const std::string& line(std::size_t i) const { return lines.at(i); } // read-only access
private:
std::string title;
std::vector<std::string> lines;
mutable int print_count = 0;
};
void show(const Report& r) { // takes const&: only const member functions allowed
r.print();
// r.add_line("x"); // error: add_line is not const
}
int main() {
Report r("Draft");
r.set_title("November").add_line("Food: 4200").add_line("Rent: 15000").add_line("Travel: 1800");
show(r);
r.line(0) = "Food: 4500"; // non-const overload
const Report& view = r;
std::cout << "first line via const view: " << view.line(0) << '\n'; // const overload
show(r);
std::cout << "lines: " << r.size() << ", printed " << r.times_printed() << " times\n";
std::cout << "same object? " << std::boolalpha << r.same_object(view) << '\n';
return 0;
}
/* Output:
== November ==
Food: 4200
Rent: 15000
Travel: 1800
first line via const view: Food: 4500
== November ==
Food: 4500
Rent: 15000
Travel: 1800
lines: 3, printed 2 times
same object? true
*/