Excluding a class from a namespace (c++)

Started by
3 comments, last by Servant of the Lord 15 years, 8 months ago
Is there a way to use a namespace, but exclude certain classes? ie. using namespace std; not using std::string if for example I had my own string class.
Advertisement
Nope. The best you can do is specify everything you're using out of the namespace (using std::vector; for example). using namespace is usually considered poor practice in certain situations, such as in the global namespace, due to namespace pollution.
Ra
As you learn more about C++ you'll soon realize that using entire namespaces is unnecessary and usually not recommended. For example, if your code only uses cout and string from the std namespace, don't include the whole namespace. Instead, include only what you need, i.e.

using std::string;
using std::cout;

With that said, if you have your own string class, then simply don't use std::string.

For example, your string class is in the namespace mine, then you would:

using std::cout;
using mine::string;

Ok, cheers for the replies. What I posted was just an example, in my actual project I'm using Irrlicht (which has about 6 namespaces) and I'm using pretty much everything in the 'core' namespace except for string, so it was a little annoying needing to state all the other classes explicitly
Quote:Original post by Zenix
Ok, cheers for the replies. What I posted was just an example, in my actual project I'm using Irrlicht (which has about 6 namespaces) and I'm using pretty much everything in the 'core' namespace except for string, so it was a little annoying needing to state all the other classes explicitly

If you are using std::string, and it's std::string that's classing with irr::string, type out std::string each time you need to use it.

If it's your own string, that's not in a namespace, type out '::string' to use your own string, and 'string' to use irr::string, if you have using namespace irr;

This topic is closed to new replies.

Advertisement