C++ Static Data and Member Functions
Static variables are global variables which are created only once in a program. They have a lifetime across the entire run of a program, but are still controlled by its scope: file, function, class.
If not explicitly initialized, they will be zero-initialized for basic types and default-initialized for objects.
Static Variables with a File Scope
Global but static variables can be only used in the current file, without external linkage.
Example:
// file: global-static.cpp
int a = 1; static int b = 2;
int c = 3; static int d = 4;
// file: test-global-static.cpp
int a; int b;
extern int c; extern int d;
int main() { return 0; }
g++ global-static.cpp test-global-static.cpp -o test-global-static
Compilation error:
multiple definition of 'a'
undifined reference to 'd'
So we can see that static variables cannot be seen by other files.
Static Variables with a Function Scope
Static variables in a function are initialized only once regardless how many times the function is called. They retain their values across function calls, and can be accessed only inside the function.
Example:
// file: function-static.cpp
#include <iostream>
void dosomething()
{
static int num_calls = 0;
std::cout << ++num_calls;
}
int main() { for (int i = 0; i < 3; i++) dosomething(); }
g++ function-static.cpp -o function-static
Execution result: 123
Static Variables with a Class Scope
Static class data members are actually global variables specified by the keyword static
under the scope of a class. They are shared among all objects of that class.
Static variables exist even when there are no objects of the class, and they do not take up space inside an object.
Static variables cannot be initialized in the class definition, except for const/enum static data. They must be defined outside the class definition, usually in the class implementation (.cpp) file.
One still has to observe their access and const qualifier.
Example:
// file: class-static-data.cpp
#include <iostream>
class A {
private: static int num_instances; // No initialization should be done if not const
public: A() { std::cout << ++num_instances; }
};
int A::num_instances = 0; // necessary, otherwise undefined
int main() {
A a1; A a2; A a3;
return 0;
}
g++ class-static-data.cpp -o class-static-data
Execution result: 123
Static Class Member Functions
Static data member / methods are also called class data / methods. They are actually global variables / functions but with a class scope and are subject to the access control specified by the class developer.
Static member functions can be called like a global function by using the class scope operator ::
, or like a member function of the class using .
operator.
Still have to observe their access control.
Static member functions belong to a class, not to a particular object of the class. Therefore, static methods of a class
- do not have the implicit
this
pointer like regular non-static member functions. - may be used even when there are no objects of the class.
- can only make use of static data members of the class.
- cannot be const nor virtual functions.
- cannot be overloaded with a non-static member function of the same prototype (will throw a compilation error).
Example:
// file: class-static-method.cpp
#include <iostream>
class A {
public:
static void sayGG() { std::cout << "GG"; }
};
int main () {
A a;
A::sayGG();
a.sayGG();
return 0;
}
g++ class-static-method.cpp -o class-static-method
Execution result: GGGG