How to Rename Namespace?

23 11 2008

Namspaces are introduced to have logical grouping of classes. But as the framework grows you could often find that the namespace length grows which make the usage more difficult.


For instance, one namespace i’ve encountered is like this –

namespace Company
    namespace Product
        namespace Communication
            namespace Event
                class CEventEx

How much I’ve to go inside to address a class? Well, in big frameworks this kind of deep hierarchy is unavoidable. But, its a fact that it causes trouble to developer who uses it. Well, is there any idea to avoid that?

Well, you can rename the namespace to much more smaller one! Have a look at the code snippet below.

// Rename the namespace
namespace ThirdPartyEvent = Company::Product::Communication::Event;
// Create the object by using renamed namespace.
ThirdPartyEvent::CEventEx objEvent;

There are still a lot of gems inside C++. isn’t it? 😉

Targeted Audience – Beginners.