Static members

Static methods

A static method can not access the (non-static) members of the class it belongs to. It can be seen as a function defined inside the namespace the class. A static method can be either called like we would call a function or from an object, like a normal method. If it is called like a function, its name must be prefixed by the namespace of the class.

Defining static methods

To make a method static, all we have to do is write the keyword static before its declaration or, if it is defined inside the class definition, before its definition.

Example with the definition of a static method inside the class definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> class MyClass { public: static void printHello() { std::cout << "Hello!" << std::endl; } }; int main() { // Calling a static method as a function. MyClass::printHello(); MyClass myClass; // Calling a static method from an object. myClass.printHello(); return 0; }

Example with the definition of the static method outside the class definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> class MyClass { public: static void printHello(); }; void MyClass::printHello() { std::cout << "Hello!" << std::endl; } int main() { MyClass::printHello(); MyClass myClass; myClass.printHello(); return 0; }

Static member variables

The concept of static member variables is similar to the concept of static methods: It is a variable that belongs to the class, rather than the individual objects. Those members can be seen as global variables defined under the namespace of the class. The advantage of static member variables is that there is always only one copy of that variable, so even if there are 500 objects of the class, there is still only one copy, in the memory, of that variable.

Defining static member variables

To define a static member variable, first, we must add the keyword static, in its declaration (Inside the class definition), before its type.

static int staticMember;

However, this only has the effect of declaring the member variable (Similar to declaring a variable with the keyword extern). We have to define it outside the class, the same way we would declare a global variable, but by prefixing the name of the variable with the namespace of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> class A { public: int member; // Declares the static member. static int staticMember; }; // Defines the static member. int A::staticMember = 0; class B { public: int member; }; int main() { // Static member variables do not increase the size of the objects of the class. std::cout << "Sizeof(A): " << sizeof(A) << std::endl; std::cout << "Sizeof(B): " << sizeof(B) << "\n\n"; std::cout << "A::staticMember: " << A::staticMember << "\n"; A obj; std::cout << "obj.staticMember: " << obj.staticMember << "\n"; obj.staticMember = 123; std::cout << "A::staticMember: " << A::staticMember << "\n"; return 0; }

Since static member variables are similar to global variables, they usually are defined as constants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> class A { public: static const double PI; }; const double A::PI = 3.14159265358979; int main() { std::cout << "PI: " << A::PI << std::endl; return 0; }

Since C++11, if a static member variable is constant (And defined with the keyword constexpr), it is possible to directly define it inside the class definition. This is done the same way we would declare it, but by assigning it a value at the same time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> class A { public: // Because the variable 'PI' is constant, we can define it here. constexpr static const double PI = 3.14159265358979; }; int main() { std::cout << "PI: " << A::PI << std::endl; return 0; }