Jump to content
  • Advertisement
Sign in to follow this  
Zenix

Excluding a class from a namespace (c++)

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!