This is a question that troubles me a for a while, but could not find a the best way to deal with it. I am trying to show this through an example.
I am developing a graphics library with many classes. Some of the classes are "part of" relationship with each other, like these 3 classes:
namespace MyGraphicsLibrary
{
class MatrixStack
{
};
class Transform
{
MatrixStack mMatrixStack;
};
class Renderer
{
Transform mTransform;
};
}
The Renderer class is for users to use, but i did not want them to see Transform, MatrixStack classes when they lookup the MyGraphicsLibrary. The last two classes are only for Renderer class and not for users to use.
Here i am trying to do two things:
Hiding the
Transform,MatrixStackclasses from users.Reflect the "part-of" hierarchy of the classes.
I tried the followings to solve this:
The best solution for me would be the private nested-classes, as it would show the user that the nested class is private and also reflects the hierarchy if you simply look at the
Rendererclass declaration. The following post actually makes me uncertain that is good solution: Pros and cons of using nested C++ classes and enumerations?I tried to put
Transform,MatrixStackinto another namespace calledPrivate. So user looking upMyGraphicsLibrarynamespace would seePrivatenamespace only covering all classes which are not for the users. That's good, but there are lot of other classes with the same issue, and i quickly fill thePrivatenamespace with classes which has nothing to do with each other. Here I could only come up with ugly solutions, like introducing nested namespaces:namespace MyGraphicsLibrary { //private classes belonging to Renderer class namespace PrivateRenderer { class MatrixStack { }; class Transform { MatrixStack mMatrixStack; }; } //public classes for users class Renderer { Transform mTransform; }; }
Maybe I miss something here, but what do you think which one is the way to go. Does anybody has a 3rd way?