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.
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 शामिल हैं।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में static member क्या है?
static keyword से घोषित, static data member हर instance के लिए एक साझा value रखता है, और static member function बिना object के call हो सकता है।C++ में static और non-static member में क्या अंतर है?
C++ में static data member कैसे परिभाषित करते हैं?
static से declare करते हैं, फिर storage आवंटित करने को class के बाहर एक बार परिभाषित करते हैं, जैसे int Counter::count = 0;। उस class-बाहर परिभाषा के बिना program link नहीं होता, क्योंकि member को कहीं वास्तविक storage चाहिए।C++ में static member function क्या है?
Math::square(5)। चूँकि यह किसी object से बँधा नहीं, इसके पास this pointer नहीं होता और यह केवल static data members सीधे इस्तेमाल कर सकता है।क्या C++ में static member function non-static members access कर सकता है?
this pointer नहीं होता, तो यह non-static (per-object) data को refer या अपने आप non-static functions call नहीं कर सकता। यह केवल static members के साथ काम कर सकता है जब तक आप इसे इस्तेमाल को कोई object न दें।