Static Members in C++
In this lesson: Static variables and static member functions in C++ classes — object counter example with output. In Hindi & English.
What is a static member?
Normally each object has its own copy of every member. A static member is different: it belongs to the class itself, so there is just one copy shared by all objects. It is perfect for something common to every instance — like a running count of how many objects exist.
Static data members
Declare a data member static and every object shares it:
class Counter {
public:
static int count; // shared by all objects
};Defining a static member
A static data member must also be defined once outside the class to give it storage:
int Counter::count = 0; // definition outside the class
Forgetting this out-of-class definition causes a linker error.
Static member functions
A static member function can be called on the class without any object. It can use static members, but not per-object data:
class Math {
public:
static int square(int x) { return x * x; }
};
int main() {
cout << Math::square(5); // 25 — no object needed
}25
Example: object counter
A classic use — count how many objects have been created:
#include <iostream>
using namespace std;
class Widget {
public:
static int count;
Widget() { count++; } // every object bumps the shared count
};
int Widget::count = 0;
int main() {
Widget a, b, c;
cout << "Widgets: " << Widget::count << "\n";
return 0;
}Widgets: 3
Accessing without an object
Static members are reached through the class name and the scope operator: Widget::count, Math::square(5). You can also use an object (a.count), but the class-name form makes the "shared" nature clear.
Common mistakes
- Forgetting the out-of-class definition of a static data member (linker error).
- Trying to use non-static members inside a static function.
- Expecting each object to have its own copy of a static member.
- Redefining the static member inside a header included in many files.
Add a static count to a class that increments in the constructor and decrements in the destructor. Create and destroy objects in different scopes and print the live count to watch it rise and fall.
Summary
- A static member belongs to the class, shared by all objects.
- Static data members need a single definition outside the class.
- Static member functions can be called without an object.
- A static function has no
thisand cannot use per-object data. - Common uses include shared counters and class-wide utilities.
Frequently Asked Questions
What is a static member in C++?
static keyword, a static data member holds one value common to every instance, and a static member function can be called without an object.What is the difference between a static and a non-static member in C++?
How do you define a static data member in C++?
static, then define it once outside the class to allocate storage, such as int Counter::count = 0;. Without that out-of-class definition the program fails to link, because the member needs actual storage somewhere.What is a static member function in C++?
Math::square(5). Because it is not tied to an object, it has no this pointer and can only use static data members directly.Can a static member function access non-static members in C++?
this pointer, so it cannot refer to non-static (per-object) data or call non-static functions on its own. It can only work with static members unless you pass it an object to use.