Sign in to follow this  
Quat

C++ Private class?

Recommended Posts

Quat    568
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.

Share this post


Link to post
Share on other sites
ApochPiQ    23003
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.

Share this post


Link to post
Share on other sites
wqking    761
Or just put your internal stuff to an internal namespace, such as,

[code]
namespace publicns {
namepsace _internal { // or _private
}
}
[/code]

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.

Share this post


Link to post
Share on other sites
sam_hughes    132
[quote name='wqking' timestamp='1310434866' post='4834086']
[code]
namespace publicns {
namepsace _internal { // or _private
}
}
[/code]
[/quote]

Really you can just name it "internal", and forget about the annoying underscore.

Share this post


Link to post
Share on other sites
wqking    761
[quote name='sam_hughes' timestamp='1310438259' post='4834105']
Really you can just name it "internal", and forget about the annoying underscore.
[/quote]

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.

Share this post


Link to post
Share on other sites
Neilo    290
Why name an internal only namespace?

[code]
// In your .cpp file
namespace
{
// Stuff that's private
}
[/code]

If it's something you only want used internally, use an internal only header.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this