How make a class private inside a namespace in C++ and prevent other to access the class from outside the namespace, for instance:
namespace company{
class MyPublicClass{ ... } // This should be accessible
class MyPrivateClass{ ... } // This should NOT be accessible
}
You can't have access specifiers for namespaces, but you can for classes:
class company {
// Private (default)
class MyPrivateClass { ... };
public:
class MyPublicClass { ... };
};
You access the classes just like for a namespace with the scope operator:
company::MyPublicClass my_public_object;
If the "public" class should have access to the "private" class, then the "private" class have to be a friend
of the `public" class.
There is also another way, and that is to simply not have the MyPrivateClass
definition in a public header file. Either define the class in the source file, or in a private header file only included internally.
Which way to choose depends on your design and use-cases.