Skip to main content
gamedev.net
🔒 Locked

Namespaces

Started by Vanz Apr 14, 2010 at 1:27 PM 9 replies 1.6k views
Original Post
Vanz
Vanz
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
kaktusas2598
kaktusas2598
i also don't see too much power in thems... :D but std namespace is cool ;)
Deltron Zero and Automator.
TheBuzzSaw
TheBuzzSaw
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.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Bimble Bob
Bimble Bob
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).
It's not a bug... it's a feature!
demonkoryu
demonkoryu
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.
rip-off
rip-off
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.
tori
tori
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.
diablos_blade
diablos_blade
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.
Vanz
Vanz
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...
no such user
no such user
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.
phresnel
phresnel
Quote:
Original post by no such user
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.


And even if your program links, it might have problems with linking at runtime (shared libraries, dll's).

I would suggest to write a thin wrapper around badlib, where you just #include badlib in some isolated unit of compilation, and in the rest of the code use your wrappers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.