In C++, a namespace is a named scope or container that is used to organize and enclose a collection of code elements, such as variables, functions, classes, and other namespaces. They are mainly used to divide and manage the code base, giving developers control over name collisions and the specialization of code.
Here's the syntax for declaring a namespace:
namespace identifier {
// code elements
}
To access elements within a namespace, you can use the scope resolution operator ::. Here are some examples:
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
std::cout << "Dog's name: " << animals::dog << '\n';
std::cout << "Cat's name: " << animals::cat << '\n';
return 0;
}
Namespaces can be nested within other namespaces:
#include <iostream>
namespace outer {
int x = 10;
namespace inner {
int y = 20;
}
}
int main() {
std::cout << "Outer x: " << outer::x << '\n';
std::cout << "Inner y: " << outer::inner::y << '\n';
return 0;
}
using KeywordYou can use the using keyword to import namespaced elements into the current scope. However, this might lead to name conflicts if multiple namespaces have elements with the same name.
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
using animals::dog;
std::cout << "Dog's name: " << dog << '\n';
return 0;
}
#include <iostream>
namespace animals {
std::string dog = "Bobby";
std::string cat = "Lilly";
}
int main() {
using namespace animals;
std::cout << "Dog's name: " << dog << '\n';
std::cout << "Cat's name: " << cat << '\n';
return 0;
}
In conclusion, namespaces are a useful mechanism in C++ to organize code, avoid naming conflicts, and manage the visibility of code elements.