Home | Projects | Notes > C++ Programming > Using const
& static
with Classes
const
& static
with Classes
const
with ClassesYou can pass arguments (e.g., pointers or reference) to class member methods as const
, and they can't be modified while in the methods.
We can also create const
objects - It's attributes cannot change.
What happens if we call member functions on const
objects?
const
- Correctness
const
object
xxxxxxxxxx
111const Player villain {"Villain", 100, 55};
2
3void display_player_name(const Player &p)
4{
5 cout << p.get_name() << endl;
6}
7
8// What happens when you call member methods on a const object?
9villain.set_name("Nice guy"); // ERROR
10std::cout << villain.get_name() << std::endl; // ERROR
11display_player_name(villain); // ERROR
Once an object is created with
const
qualifier, its members cannot be modified.But, what's up with the
get_name()
function? Why is the compiler giving an error when the function is simply returning the name without modifying it?
It's because the compiler assumes that the get_name()
function could potentially change the object!To solve this issue, you need to declare a function as
const
to assure the compiler that the function will not modify the object and therefore it is safe to call.
const
methods
xxxxxxxxxx
131class Player
2{
3private:
4 . . .
5public:
6 std::string get_name() const;
7 // Compiler will generate an error if code in get_name modifies this object
8 . . .
9};
10
11const Player villain {"Villain", 100, 55};
12villain.set_name("Nice guy"); // ERROR
13std::cout << villain.get_name() << std::endl; // OK
Now the compiler will not only allow this method to be called on
const
objects, it will also produce a compiler error if you try to modify any of the object attributes in the method.Check out the concept of "Const-Correctness".
static
Class MembersWhen we declare class data members as static
, we're telling the C++ compiler that these data members belong to the class, not to any specific object.
Useful to create and store class-wide information
e.g., How many player objects do we have active in our application at any point in time? static
variable as a member of the Player
class and then increment it and decrement it in the code whenever we create an object or delete an object.
Class functions can be declared as static
Independent of any objects
Can be called using the class name
Since static
, they only have access to static data members. They do not have access to non-static class data members.
Player
class - static
members
xxxxxxxxxx
111/* Player.h */
2
3class Player
4{
5private:
6 static int num_players; // Cannot be initialized here
7
8public:
9 static int get_num_players();
10 . . .
11}
Initializing the static
data
xxxxxxxxxx
51/* Player.cpp */
2
3
4
5int Palyer::num_players = 0;
L5: Initializing a
static
member variable must happen EXACTLY once, and it typically happens in the.cpp
file for the class.
Implementing static
method
xxxxxxxxxx
41int Player::get_num_players()
2{
3 return num_players;
4}
Note that
static
member methods only have access tostatic
data members. They do not have access to non-static class data members.
Player
class - Update the constructor and destructor
xxxxxxxxxx
101Player::Player(std::string name_val, int health_val, int xp_val)
2 : name{name_val}, health{health_val}, xp{xp_val}
3{
4 ++num_players;
5}
6
7Player::~Player()
8{
9 --num_players;
10}
Be careful! If you have many overloaded constructors since you might have to increment
num_players
in more than one place. Basically, anywhere you create an object.We only have one constructor in the
Plyaer
class and we're delegating to it from the copy constructor. So this is the only place we want to incrementnum_players
in this example.
main.cpp
xxxxxxxxxx
151/* main.cpp */
2
3void display_active_players()
4{
5 cout << "Active players: " << Player::get_num_players() << endl;
6}
7
8int main(void)
9{
10 display_active_players(); // 0
11
12 Player obj1{"Jack"};
13 display_active_players(); // 1
14 . . .
15}