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

C++ के Static Members

एक static member हर object की बजाय पूरी class से belong करता है, wall पर लिखे एक class rule की तरह जिसे सब share करते हैं। इसकी सिर्फ एक 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 एक class के हर instance में shared होते हैं — memory में एक static member variable की सिर्फ हमेशा एक copy होती है चाहे आप कितने भी objects बनाएं, ordinary member variables के उलट जिनकी हर object को अपनी copy मिलती है।

उदाहरण: 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;
}

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

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 को कोई object बनाए बिना सीधे class पर ही call किया जा सकता है, ClassName::function() syntax इस्तेमाल करके।

क्योंकि वे किसी specific instance से tied नहीं हैं, वे सिर्फ दूसरे static data को touch या दूसरे static functions को call कर सकते हैं।

उदाहरण: 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;
}

Static Functions में Access Restrictions

Static member functions को एक this pointer नहीं मिलता, क्योंकि वे किसी particular object से associated नहीं हैं, जिसका मतलब है उनके पास non-static member variables तक पहुँचने का बस कोई तरीका नहीं — एक access करने की कोशिश एक compile error है।

उदाहरण: 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 आमतौर पर एक class के सारे instances में shared state track करने के लिए इस्तेमाल होते हैं, जैसे currently कितने objects exist करते हैं इसकी एक running count, एक shared configuration setting, या एक globally unique ID generator जो अगली available key देता है।

उदाहरण: 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. #}
आम गलतियां
  1. class के बाहर एक static data member define न करना।
  2. एक static member function के अंदर this इस्तेमाल करना।
  3. एक static function से non-static members access करना।

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.