Public Group

Namespaces

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

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 on other sites
i also don't see too much power in thems... :D but std namespace is cool ;)

Share on other sites
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 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 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 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 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 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 on other sites
Quote:
 Original post by Dom_152Namespaces 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 on other sites
Quote:
 Original post by toriIf you need to deal with a namespace-unaware library that clashes with your names, you can use this:

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• Forum Statistics

• Total Topics
633757
• Total Posts
3013711
×