How can I avoiding typing namespace:: overly often in my code?

c++, cocos2d-iphone

Solution

There are several ways to use stuff from other namespaces without having to repeat the namespace on every instance.

- Import the entire namespace: `using namespace cocos2d;` You can now use all members of that namespace by only their name without the namespace prefix. This pollutes your own namespace with possibly quite a few names (depending on the contents of the imported namespace) which might not be desirable.

- Import single names from that namespace: `using cocos2d::MyClassName;` This only imports the given name. The upside is that your namespace is not polluted. The downside is that you will have to do it for every namespace member you want to import. If you only need a few then this approach is fine.

- Create a namespace alias: `namespace co = cocos2d;` Now you can refer to members of the `cocos2d` namespace as if they were members of the `co` namespace.

- Create a type alias (since C++11): `using CoClass = cocos2d::MyClassName;` You can then refer to the aliased member with the identifier you chose. This can be especially helpful when an imported type shadows a type in your own namespace.

Problem

I use cocos2dx. When I use classes from it I need to type `cocos2d::` very often unless I type `using namespace cocos2d;`. How can I avoid having to repeat the namespace all the time?

Original source

Related problems