C++ Private class?

Started by
6 comments, last by bubu LV 12 years, 9 months ago
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.
-----Quat
Advertisement
Well the easiest method would just be not to put the class in your public headers.
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Or just put your internal stuff to an internal namespace, such as,


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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.



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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Why name an internal only namespace?


// 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