C++ के Static Members
In this page:
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 एक class के हर instance में shared होते हैं — memory में एक static member variable की सिर्फ हमेशा एक copy होती है चाहे आप कितने भी objects बनाएं, ordinary member variables के उलट जिनकी हर object को अपनी copy मिलती है।
उदाहरण: Static Data Members
#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;
}
Login to try C/C++/Java/PHP code in the editor
Static Members Initialize करना
Regular members के उलट, एक static data member को class body के बाहर explicitly define और initialize करना ज़रूरी है, आमतौर पर एक single source file में global/namespace scope पर, क्योंकि class declaration इसे सिर्फ declare करती है — यह actually इसके लिए storage allocate नहीं करती।
उदाहरण: Initializing Static Members
#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;
}
Login to try C/C++/Java/PHP code in the editor
Static Member Functions
Static member functions को कोई object बनाए बिना सीधे class पर ही call किया जा सकता है, ClassName::function() syntax इस्तेमाल करके।
क्योंकि वे किसी specific instance से tied नहीं हैं, वे सिर्फ दूसरे static data को touch या दूसरे static functions को call कर सकते हैं।
उदाहरण: Static Member Functions
#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;
}
Login to try C/C++/Java/PHP code in the editor
Static Functions में Access Restrictions
Static member functions को एक this pointer नहीं मिलता, क्योंकि वे किसी particular object से associated नहीं हैं, जिसका मतलब है उनके पास non-static member variables तक पहुँचने का बस कोई तरीका नहीं — एक access करने की कोशिश एक compile error है।
उदाहरण: Access Restrictions in Static Functions
#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;
}
Login to try C/C++/Java/PHP code in the editor
Real-World Static Use Cases
Static members आमतौर पर एक class के सारे instances में shared state track करने के लिए इस्तेमाल होते हैं, जैसे currently कितने objects exist करते हैं इसकी एक running count, एक shared configuration setting, या एक globally unique ID generator जो अगली available key देता है।
उदाहरण: Real-World Static Use Cases
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- class के बाहर एक static data member define न करना।
- एक static member function के अंदर
thisइस्तेमाल करना। - एक static function से non-static members access करना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: