Namespaces

Started by
9 comments, last by phresnel 14 years ago
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
Advertisement
i also don't see too much power in thems... :D but std namespace is cool ;)

Deltron Zero and Automator.

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.
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!
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.
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.
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.
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.
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...
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.

This topic is closed to new replies.

Advertisement