obj.setSpeed(120);, the compiler transforms the call into:
Car::setSpeed(&obj, 120); // Hidden 'this' pointer passed as first parameterobj.setSpeed(120);, the compiler transforms the call into:
Car::setSpeed(&obj, 120); // Hidden 'this' pointer passed as first parameterReturning *this by reference (ClassName&) enables sequential chained calls: query.select().from().where();.
#include <iostream>
#include <string>
class QueryBuilder {
private:
std::string query;
public:
QueryBuilder() = default;
// Fluent API returning *this reference
QueryBuilder& select(const std::string& fields) {
this->query = "SELECT " + fields; // 'this->' disambiguates
return *this;
}
QueryBuilder& from(const std::string& table) {
this->query += " FROM " + table;
return *this;
}
QueryBuilder& where(const std::string& condition) {
this->query += " WHERE " + condition;
return *this;
}
void execute() const {
std::cout << "Executing SQL: " << this->query << ";\n";
}
};
int main() {
QueryBuilder qb;
// Method chaining via 'return *this'
qb.select("id, username, email")
.from("users")
.where("is_active = 1")
.execute();
return 0;
}*this by reference (Type&) when implementing fluent method chaining or assignment operators (operator=).this-> when member variables and parameters share identical names in constructors and setters.this unless implementing specialized intrusive reference counting systems.