Jump to content
  • Advertisement
Sign in to follow this  
Vanz

Namespaces

This topic is 2987 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

I don't see the power in Namespaces... Namespaces @ cplusplus.com I see how they work I just don't see how to use them efficiently, could someone please enlighten me... code is always nice... Thanks, rhuala

Share this post


Link to post
Share on other sites
Advertisement
Much of the time, you will not use namespaces in your own code. Namespaces are best for when you are releasing some kind of library for use in other programs. In those cases, namespaces are really important to prevent name collisions because you never know what the end user's naming conventions are.

Share this post


Link to post
Share on other sites
Namespaces are used to define a named scope, usually to prevent naming clashes.

Say you're using a library in a C++ program and you have a class called "FileReader". If the library you're using also has a class called "FileReader" you've got a clash.

However if all the library's classes are wrapped up into a namespace, say, "Engine", then your version of "FileReader" and the library's can coexist as they exist in different namespaces so you can resolve any ambiguity using the scope resolution operator (Engine::FileReader vs FileReader).

Share this post


Link to post
Share on other sites
It's just an instrument to organize large codebases.

An example:

Let's assume you want to use two libraries in a program, maybe a 3D library and an image loader library. Both use a base class from which all their classes inherit. Let's call this class BaseObject (just for the sake of argument).
If they don't use namespaces, the two libraries wouldn be difficult to use together. You would have to expend additional work to use them together in your project.
If they use namespaces for all their functionality, this problem doesn't exist.

That's more or less it. In a way, namespace have similarities with domain names. Java packages, for example, employ real domain names that are actually part of the "namespace" of classes.

Share this post


Link to post
Share on other sites
You'll know when you need them. The first sign is an urge to put some prefix on a bunch of related types and functions. If you find yourself fighting this urge, use a namespace.

Until then I wouldn't worry too much about them. But in time I think you'll find it handy that the Standard C++ Library lives in a separate namespace, and irritating that C libraries put all their stuff in the global namespace.

Share this post


Link to post
Share on other sites
If you need to deal with a namespace-unaware library that clashes with your names, you can use this:


namespace badlib {
#include <badlib.h>
}


(edit) Note that macros (#defines) will still be global.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
... and irritating that C libraries put all their stuff in the global namespace.


Particularly the Win32 API. LoadImage and CreateWindow spring to mind as functions that I have wanted to implement in the past, only to find there are a couple of macros with those names. At least you can define NOMINMAX to get rid of the min and max defines.

Other OS's may have similar irritations of course, but I have yet to deal with those.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
Namespaces are used to define a named scope, usually to prevent naming clashes.

Say you're using a library in a C++ program and you have a class called "FileReader". If the library you're using also has a class called "FileReader" you've got a clash.

However if all the library's classes are wrapped up into a namespace, say, "Engine", then your version of "FileReader" and the library's can coexist as they exist in different namespaces so you can resolve any ambiguity using the scope resolution operator (Engine::FileReader vs FileReader).


ah yes, this makes the most sense to me...

Thanks for the replies...

Share this post


Link to post
Share on other sites
Quote:
Original post by tori
If you need to deal with a namespace-unaware library that clashes with your names, you can use this:


Be careful, this may prevent your program from linking.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!