C++ Private class?
In C# you can mark a class "internal" so that it is only visible to other code inside the same assembly. If I am writing a .lib project in C++, is there a similar way to control what classes client code can access. I want to expose "high-level" interface classes the client works with that internally use lower level classes. It would be nice if the client code could not access lower level classes.
There are all kinds of techniques for this, with the best being (as SiCrane noted) to simply not expose the class definition in the library headers.
Can you describe the exact relationship between the classes you want to be public, and the internal stuff? I can give some specific tips if I know how things need to work, roughly speaking.
Can you describe the exact relationship between the classes you want to be public, and the internal stuff? I can give some specific tips if I know how things need to work, roughly speaking.
Or just put your internal stuff to an internal namespace, such as,
For normal users, they won't bother with _internal.
Of course you can't prevent from hacking by access publicns::_internal, same as you can't prevent from accessing your private fields by define private to public before including your header.
namespace publicns {
namepsace _internal { // or _private
}
}
For normal users, they won't bother with _internal.
Of course you can't prevent from hacking by access publicns::_internal, same as you can't prevent from accessing your private fields by define private to public before including your header.
namespace publicns {
namepsace _internal { // or _private
}
}
Really you can just name it "internal", and forget about the annoying underscore.
Really you can just name it "internal", and forget about the annoying underscore.
You answered why I use the underscore.
Without the underscore, the user may accidentally think "internal" is just a normal nested namespace.
With the underscore, the user may stop and think, why the name is so weird and annoying? Oh, it's for internal usage, I should not use it.
That's more a kind of personal style.
Boost uses something like "detail" without underscore, which works quite well.
Why name an internal only namespace?
If it's something you only want used internally, use an internal only header.
// In your .cpp file
namespace
{
// Stuff that's private
}
If it's something you only want used internally, use an internal only header.
Naming internal namespace is needed if it needs to be in the public header file, like boost does.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement