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

C++ Static Members

A static member belongs to the whole class instead of each object, like a class rule written on the wall that everyone shares. There is only one copy.
Syntax
cpp
class ClassName {
public:
  static data_type member_name;
};

data_type ClassName::member_name = value;  // defined outside the class

ClassName::member_name;

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 std::cout and std::cin
#include <iostream>

// Define the type Widget
class Widget {
// Members below can be accessed from outside the class
public:
	static int instanceCount;
	Widget() { instanceCount++; }
};

int Widget::instanceCount = 0;

// Program execution starts in main()
int main() {
	Widget a, b, c;
	// Print to the console with cout
	std::cout << Widget::instanceCount << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not defining a static data member outside the class.
  2. Using this inside a static member function.
  3. Accessing non-static members from a static function.

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.