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
xxxxxxxxxx111const 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"); // ERROR10std::cout << villain.get_name() << std::endl; // ERROR11display_player_name(villain); // ERROROnce an object is created with
constqualifier, 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
constto assure the compiler that the function will not modify the object and therefore it is safe to call.
const methods
xxxxxxxxxx131class Player2{3private:4 . . .5public:6 std::string get_name() const;7 // Compiler will generate an error if code in get_name modifies this object8 . . .9};10
11const Player villain {"Villain", 100, 55};12villain.set_name("Nice guy"); // ERROR13std::cout << villain.get_name() << std::endl; // OKNow the compiler will not only allow this method to be called on
constobjects, 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
xxxxxxxxxx111/* Player.h */2
3class Player4{5private:6 static int num_players; // Cannot be initialized here7
8public:9 static int get_num_players(); 10 . . .11}Initializing the static data
xxxxxxxxxx51/* Player.cpp */2
34
5int Palyer::num_players = 0;L5: Initializing a
staticmember variable must happen EXACTLY once, and it typically happens in the.cppfile for the class.
Implementing static method
xxxxxxxxxx41int Player::get_num_players()2{3 return num_players;4}Note that
staticmember methods only have access tostaticdata members. They do not have access to non-static class data members.
Player class - Update the constructor and destructor
xxxxxxxxxx101Player::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_playersin more than one place. Basically, anywhere you create an object.We only have one constructor in the
Plyaerclass and we're delegating to it from the copy constructor. So this is the only place we want to incrementnum_playersin this example.
main.cpp
xxxxxxxxxx151/* 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(); // 011 12 Player obj1{"Jack"};13 display_active_players(); // 114 . . .15}