C++ Static Members
In this page:
Static Data Members
Static data members are shared across every instance of a class — there's only ever one copy of a static member variable in memory no matter how many objects you create, unlike ordinary member variables which each object gets its own copy of.
Example: Static Data Members
#include <iostream>
class Counter {
public:
static int count;
};
int Counter::count = 0;
int main() {
Counter a, b;
Counter::count = 5;
std::cout << a.count << " " << b.count << std::endl; // shared by every instance
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing Static Members
Unlike regular members, a static data member must be explicitly defined and initialized outside the class body, usually at global/namespace scope in a single source file, because the class declaration only declares it — it doesn't actually allocate storage for it.
Example: Initializing Static Members
#include <iostream>
class Counter {
public:
static int total;
};
int Counter::total = 100; // must be defined outside the class
int main() {
std::cout << Counter::total << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Member Functions
Static member functions can be called directly on the class itself without creating any object at all, using ClassName::function() syntax. Because they aren't tied to a specific instance, they can only touch other static data or call other static functions.
Example: Static Member Functions
#include <iostream>
class MathUtil {
public:
static int square(int x) { return x * x; }
};
int main() {
std::cout << MathUtil::square(5) << std::endl; // called without an object
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Access Restrictions in Static Functions
Static member functions don't receive a this pointer, since they aren't associated with any particular object, which means they simply have no way to reach non-static member variables — attempting to access one is a compile error.
Example: Access Restrictions in Static Functions
#include <iostream>
class Box {
int size = 5; // non-static member
public:
static void show() {
// std::cout << size; // compile error: no "this", cannot access non-static members
std::cout << "Static function has no access to instance data" << std::endl;
}
};
int main() {
Box::show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Real-World Static Use Cases
Static members are commonly used for tracking shared state across all instances of a class, such as a running count of how many objects currently exist, a shared configuration setting, or a globally unique ID generator that hands out the next available key.
Example: Real-World Static Use Cases
#include <iostream>
class Widget {
public:
static int instanceCount;
Widget() { instanceCount++; }
};
int Widget::instanceCount = 0;
int main() {
Widget a, b, c;
std::cout << Widget::instanceCount << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: