← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 5 of 17

C++ के Namespaces

एक namespace एक named group है जो आपके names दूसरों से separate रखता है, आपके नाम वाले एक box में अपने toys रखने की तरह। जब names match करें तो यह mix-ups से बचाता है।
Syntax
cpp
namespace NamespaceName {
  return_type function_name() { }
}

NamespaceName::function_name();

using namespace NamespaceName;

Namespace क्या है?

एक namespace C++ में एक feature है जो related code को एक unique नाम के नीचे group करने के लिए इस्तेमाल होता है। यह naming conflicts रोकने में मदद करता है। यह खासकर तब useful है जब आपका project identical class या function names वाली multiple external libraries इस्तेमाल करता है।

उदाहरण: What is a Namespace?

cpp
// Include std::cout and std::cin
#include <iostream>

namespace MyLibrary {
	void greet() { std::cout << "Hello from MyLibrary" << std::endl; }
}

// Program execution starts in main()
int main() {
	MyLibrary::greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Namespace

सारे standard library templates, classes, और objects std namespace के अंदर grouped हैं। आप scope resolution operator इस्तेमाल कर सकते हैं या standard elements को अपने current scope में import करने के लिए एक using directive लिख सकते हैं।

उदाहरण: The Standard Namespace

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Using the std namespace" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Namespaces

आप एक namespace के अंदर एक namespace declare कर सकते हैं। यह आपके code modules के लिए एक clear hierarchy establish करता है, आपके computer पर nested folders जैसा।

उदाहरण: Nested Namespaces

cpp
// Include std::cout and std::cin
#include <iostream>

namespace Company {
	namespace Project {
		void run() { std::cout << "Running" << std::endl; }
	}
}

// Program execution starts in main()
int main() {
	Company::Project::run();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Inline Namespaces

एक inline namespace आपको versioned APIs define करने देता है। एक inline namespace के members parent namespace में automatically visible होते हैं। यह आपको backward compatibility preserve करते हुए code updates deploy करने में मदद करता है।

उदाहरण: Inline Namespaces

cpp
// Include std::cout and std::cin
#include <iostream>

namespace Library {
	inline namespace V2 {
		void greet() { std::cout << "Version 2" << std::endl; }
	}
}

// Program execution starts in main()
int main() {
	Library::greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Namespaces Best Practices

Header files में global using directives डालने से बचें। यह इस header को include करने वाली किसी भी file के global scope को pollute कर सकता है, hard-to-track naming conflicts की ओर ले जाते हुए।

उदाहरण: Namespaces Best Practices

cpp
#include <iostream>

// Avoid "using namespace std;" in header files -- it would pollute the
// global scope of every file that includes this header.

int main() {
	std::cout << "Prefer explicit std:: prefixes in headers" << std::endl;
	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. एक namespace से एक function call करते वक्त :: भूल जाना।
  2. हर जगह using namespace इस्तेमाल करके naming conflicts बनाना।
  3. एक header file में using namespace std; डालना।

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.