The 'this' Pointer
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 7 of 104.
In C++, this is a pointer to the current object. Use this-> to access members, especially when parameter names match member names.
Without this, the parameter shadows the member
void Player::setName(std::string name) {
name = name; // Assigns parameter to itself! Member unchanged.
}Using this-> to correctly assign values
void Player::setName(std::string name) {
this->name = name; // Assigns parameter to member
}Using this-> in methods
std::string Player::getStatus() {
return this->name + " - Score: " + std::to_string(this->score);
}The this pointer always points to the current object instance. Unlike Java and C# where this is a reference, in C++ it's a pointer, so you use -> instead of . to access members.
Challenge
MediumImplement the Player class methods using this-> to distinguish between members and parameters with the same name:
- Setter methods: use
this->to assign parameters to members getStatus(): usethis->to access members, return"<name> - Score: <score>, Level: <level>"
Cheat sheet
In C++, this is a pointer to the current object. Use this-> to access members, especially when parameter names match member names.
Without this, the parameter shadows the member:
void Player::setName(std::string name) {
name = name; // Assigns parameter to itself! Member unchanged.
}Using this-> to correctly assign values:
void Player::setName(std::string name) {
this->name = name; // Assigns parameter to member
}Using this-> in methods:
std::string Player::getStatus() {
return this->name + " - Score: " + std::to_string(this->score);
}The this pointer always points to the current object instance. Unlike Java and C# where this is a reference, in C++ it's a pointer, so you use -> instead of . to access members.
Try it yourself
#include <iostream>
#include "Player.h"
int main() {
std::string name;
int score, level;
std::getline(std::cin, name);
std::cin >> score >> level;
Player player;
player.setName(name);
player.setScore(score);
player.setLevel(level);
std::cout << "Name: " << player.name << std::endl;
std::cout << "Score: " << player.score << std::endl;
std::cout << "Level: " << player.level << std::endl;
std::cout << player.getStatus() << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container