The this Pointer and const Methods
In the Constructors lesson, the parameter had a different name from the member: Pet(string n) stored n in name. Often you want to use the same name for both, because it is the clearest name. Here is what happens if you do:
#include <iostream>
#include <string>
using namespace std;
class Player {
public:
string name;
Player(string name) {
name = name;
}
};
int main() {
Player p("Ana");
cout << "[" << p.name << "]" << "\n";
return 0;
}
Output:
[]
The name is empty. Inside the constructor, name means the parameter, because the parameter is the closer one. So name = name; copies the parameter into itself, and the member is never set. The compiler does not complain.
The this pointer
Inside every method, C++ gives you a pointer called this. It points to the object that the method was called on. To reach a member through a pointer, you write ->. So this->name is the member, and plain name is still the parameter:
#include <iostream>
#include <string>
using namespace std;
class Player {
public:
string name;
Player(string name) {
this->name = name;
}
};
int main() {
Player p("Ana");
cout << "[" << p.name << "]" << "\n";
return 0;
}
Output:
[Ana]
this->name means the same as (*this).name: go to the object, then take its name. You only need this-> when a parameter hides a member. In other methods, name already means the member.
const methods
In the References lesson, you saw const string&, a parameter that reads a string without copying it. You can pass your own objects the same way. But on a const object, C++ lets you call only methods that promise not to change the object. This program breaks that rule:
#include <iostream>
using namespace std;
class Player {
public:
int level = 1;
int getLevel() {
return level;
}
};
void show(const Player& p) {
cout << p.getLevel() << "\n";
}
int main() {
Player p;
show(p);
return 0;
}
Output:
main.cpp: In function ‘void show(const Player&)’:
main.cpp:14:23: error: passing ‘const Player’ as ‘this’ argument discards qualifiers [-fpermissive]
The ‘this’ argument is p, the object that getLevel is called on. "Discards qualifiers" means the call would drop the const from p. getLevel does not change anything, but the compiler does not look inside it to check. It only trusts a promise.
The promise is the word const after the parentheses. A method marked like this is a const method:
#include <iostream>
using namespace std;
class Player {
public:
int level = 1;
int getLevel() const {
return level;
}
};
void show(const Player& p) {
cout << p.getLevel() << "\n";
}
int main() {
Player p;
show(p);
return 0;
}
Output:
1
The compiler also holds the method to its promise. If a const method tries to change a member, the program does not compile:
#include <iostream>
using namespace std;
class Counter {
public:
int value = 0;
int next() const {
value++;
return value;
}
};
int main() {
Counter c;
cout << c.next() << "\n";
return 0;
}
Output:
main.cpp: In member function ‘int Counter::next() const’:
main.cpp:9:9: error: increment of member ‘Counter::value’ in read-only object
Inside a const method, the object is read-only, so value++ on line 9 is an error.
A good habit: mark every method that only reads, like a getter, as const. Then it works with const references, and the reader knows at a glance that it changes nothing.
In the exercise, use this-> in the constructor of a Book class, and make its getters const so a function that takes a const Book& can call them.