← Back to C++ Course | Chapter 8: OOP Core | Lesson 9 of 14

C++ Static Members

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.