C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
Namespaces in C++ are used to group identifiers (such as classes, objects, and functions) under a single name to avoid name clashes, especially when working with large projects or libraries. In this tutorial, we will discuss C++ namespaces, their usage, and how to create, access, and use them in your code.
To create a namespace, use the namespace
keyword followed by the namespace's name and the namespace block containing the declarations within the namespace:
namespace NamespaceName { // declarations }
Here's an example of a simple namespace:
namespace ExampleNamespace { int a = 10; void display() { std::cout << "Inside ExampleNamespace" << std::endl; } }
To access the members of a namespace, use the scope resolution operator ::
:
NamespaceName::member_name;
For our ExampleNamespace
, we can access its members like this:
#include <iostream> #include "ExampleNamespace.h" int main() { std::cout << "Value of a: " << ExampleNamespace::a << std::endl; ExampleNamespace::display(); return 0; }
using
):You can use the using
directive to bring an entire namespace into the current scope:
using namespace NamespaceName;
However, be cautious when using the using
directive, as it may introduce naming conflicts if there are similar names in different namespaces.
Here's an example of using the using
directive:
#include <iostream> #include "ExampleNamespace.h" using namespace ExampleNamespace; int main() { std::cout << "Value of a: " << a << std::endl; display(); return 0; }
using
):Alternatively, you can use the using
declaration to import specific members from a namespace. This reduces the chances of name clashes compared to using the using
directive:
using NamespaceName::member_name;
Here's an example:
#include <iostream> #include "ExampleNamespace.h" using ExampleNamespace::display; int main() { std::cout << "Value of a: " << ExampleNamespace::a << std::endl; display(); return 0; }
Namespaces can be nested inside other namespaces:
namespace OuterNamespace { namespace InnerNamespace { // declarations } }
Access the members of a nested namespace using the scope resolution operator:
OuterNamespace::InnerNamespace::member_name;
An anonymous namespace is a namespace without a name. Declarations in an anonymous namespace are directly accessible within the same translation unit, similar to static declarations. However, these declarations have external linkage, allowing them to be accessed by other translation units using a declaration with the extern
keyword.
namespace { // declarations }
That concludes our tutorial on C++ namespaces. Namespaces are a useful feature for avoiding name clashes and organizing your code, making it more maintainable and easier to work with in larger projects.
How to use namespaces in C++:
#include <iostream> // Declare a namespace named 'MyNamespace' namespace MyNamespace { int x = 5; void displayX() { std::cout << "X value: " << x << std::endl; } } int main() { // Accessing the namespace member MyNamespace::displayX(); return 0; }
Nested namespaces in C++:
#include <iostream> // Outer namespace namespace Outer { int x = 5; // Inner namespace namespace Inner { int y = 10; } } int main() { // Accessing members from nested namespaces std::cout << "X value: " << Outer::x << std::endl; std::cout << "Y value: " << Outer::Inner::y << std::endl; return 0; }
Anonymous namespaces in C++:
#include <iostream> // Anonymous namespace namespace { int z = 15; } int main() { // Accessing the anonymous namespace member std::cout << "Z value: " << z << std::endl; return 0; }
Global namespace and scope resolution operator in C++:
#include <iostream> int x = 20; // Global variable int main() { int x = 25; // Local variable with the same name as the global one // Accessing the global variable using the scope resolution operator std::cout << "Global X value: " << ::x << std::endl; return 0; }
Namespace aliasing in C++:
#include <iostream> namespace OriginalNamespace { int a = 30; } // Namespace alias namespace AliasNamespace = OriginalNamespace; int main() { // Accessing the original and aliased namespaces std::cout << "Original A value: " << OriginalNamespace::a << std::endl; std::cout << "Aliased A value: " << AliasNamespace::a << std::endl; return 0; }