An alternative to "namespaces"

Started by
10 comments, last by _Silence_ 7 years, 6 months ago

If you want to avoid using namespaces, but retain a sense of structure, then C++ allows the use of nested classes. Just be sure to make the declaration public, so you can access it from outside the scope:


class IGame {
  public:
   struct scene_t {
     ...
     };
};

I rarely use this myself (in fact, I limit nested classes to a more local scope to encapsulate logic that is and should be limited to the parent class itself, but whose members would otherwise unnecessarily pollute the class member list):


class IGame {
  protected:
    struct properties_t {
      eGameState state;
      ...
      } properties;
 
    struct ui_t {
      eUIState state;
      ...
      } ui;
};
Advertisement

I personally believe that there is nothing fanciful in C++. So I tend to use all what is provided to me. I find namespaces as valuable as having classes, enum structs or templates. This allows to confine things and to prevent them to pollute another namespace or the global one.

And under a good IDE, namespaces are more helpful and lead to a good completion help.

Prefix can be good, but this was prior to C++ 98. We have namespaces since then. After all, a string is a string. So why naming it MyGameNameString ? It's also the same for vectors and many other types.

This was just my two cents...

This topic is closed to new replies.

Advertisement