Reputation: 1723
Is it possible to make variables or functions to be public for multiple files in same namespace, but private for different namespace?
For example:
a1.cpp
namespace A
{
int distance = 10;
}
a2.cpp
namespace A
{
extern int distance;
void f() { std::cout << distance; } // OK! I need this access.
}
b.cpp
namespace A
{
extern int distance;
}
namespace B
{
void f() { std::cout << A::distance; } // I would like to prevent this access!
}
Upvotes: 1
Views: 86
Reputation: 2124
No, namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. And identifiers outside the namespace can ACCESS the members by using the fully qualified name for each identifier. So you cannot prevent outsider access variable within namespace.
Upvotes: 1