Static Data Members and Static Functions
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
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.Static member क्या है?
सामान्यतः हर object के पास हर member की अपनी copy होती है। Static member अलग है: यह class के लिए ही होता है, तो केवल एक copy होती है जो सभी objects साझा करते हैं। यह हर instance के लिए साझा किसी चीज़ के लिए एकदम सही है — जैसे कितने objects मौजूद हैं इसकी चलती गिनती।
Static data members
किसी data member को static declare करें और हर object इसे साझा करता है:
class Counter {
public:
static int count; // sabhi objects dwara shared
};Static member परिभाषित करना
Static data member को storage देने के लिए class के बाहर एक बार परिभाषित भी करना होगा:
int Counter::count = 0; // class ke bahar definition
इस class-बाहर परिभाषा को भूलना linker error पैदा करता है।
Static member functions
static member function बिना किसी object के class पर call हो सकता है। यह static members इस्तेमाल कर सकता है, पर per-object data नहीं:
class Math {
public:
static int square(int x) { return x * x; }
};
int main() {
cout << Math::square(5); // 25 — object ki zaroorat nahi
}25
उदाहरण: object counter
एक classic इस्तेमाल — गिनें कि कितने objects बनाए गए:
#include <iostream>
using namespace std;
class Widget {
public:
static int count;
Widget() { count++; } // har object shared count badhata hai
};
int Widget::count = 0;
int main() {
Widget a, b, c;
cout << "Widgets: " << Widget::count << "\n";
return 0;
}Widgets: 3
बिना object access करना
Static members class नाम और scope operator से पहुँचे जाते हैं: Widget::count, Math::square(5)। आप object भी इस्तेमाल कर सकते हैं (a.count), पर class-नाम रूप "साझा" प्रकृति स्पष्ट करता है।
आम गलतियाँ
- Static data member की class-बाहर परिभाषा भूलना (linker error)।
- Static function के अंदर non-static members इस्तेमाल करने की कोशिश।
- यह उम्मीद करना कि हर object की static member की अपनी copy हो।
- कई files में शामिल header के अंदर static member को फिर से परिभाषित करना।
किसी class में एक static count जोड़ें जो constructor में बढ़े और destructor में घटे। अलग scopes में objects बनाएँ और नष्ट करें और live count print करके इसे बढ़ते-घटते देखें।
सारांश
- Static member class के लिए होता है, सभी objects द्वारा साझा।
- Static data members को class के बाहर एक परिभाषा चाहिए।
- Static member functions बिना object के call हो सकते हैं।
- Static function के पास
thisनहीं होता और per-object data इस्तेमाल नहीं कर सकता। - सामान्य इस्तेमाल में साझा counters और class-wide utilities शामिल हैं।