This topic is now archived and is closed to further replies.



Recommended Posts

A namespace is a grouping mechanism often used to reduce the likelyhood of naming conflicts. You can also do neat things like alias namespaces. Consider this:

void foo() {}
class bar { bar() {} };
namespace FooBar {
void foo() {}
class bar { bar() {} };

In this example, the function and class inside the namespace are different from the function and class outside. There are no naming conflicts. This comes in handy with large libraries.

Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Well the easiest way to explain this is to take a simple example and show you. Lets say that your taking a C++ course in school. Now your teacher assigns you and your classmates to write a string class.

So everyone in the course writes a C++ Class called "string". Now each class is called "string" but Your string class is in a file called string1.cpp/string1.h and your friend bob''s string class in in a file called string2.cpp/string2.h.

So now you create a new program and include the following:

#include "string1.h"
#include "string2.h"

Now we try to make a string:

string MyString;

Now we''ve got a problem, if both classes are named "string" how does the compiler know if it''s using the "string" class in string1.h or the "string" class in string2.h? It doesn''t! This is what name spaces are for.

A name space allows you to do something like this:

Bob::String MyString;

Now the compiler knows that we want to use the "string" class that''s in string2.h and not the one that''s in string1.h.

Now this was a pretty simplistic example, but you can see that this can be a problem.

Think of a name space as a persons full legal name rather than the nickname you call them. In certian situations when your confronted with 2 people that have the same name sometimes you need to explicitly state who your trying to talk to.

Share this post

Link to post
Share on other sites