whats namespace?

Started by
0 comments, last by mad_hatter 22 years, 3 months ago
Whats namepace?
Advertisement
A name space is something that was added to the C++ language in order to easily let people inter mix libraries.

Lets say you''ve downloaded 2 math libraries for your game.

"MathLibrary" and "MathPlus". Each of these libraries have functions to do multiplication, and other types of math. So they have many of the same function names.

Example:
MathLibrary''s multiply function looks like this:
Multiply(int value1, int value2, int &result);

MathPlus''s multiply function looks like this:
Multiply(int value1, int value2, int &result);

However lets say that by testing the two libraries out you''ve determined that MathLibraries divide function is 2x faster than MathPlus''s but MathPlus''s multiply is 2x faster than MathLibraries.

You''ve got a bit of a problem on your hands. You can''t include both libraries because they have the same function names and return values. This is where namespaces become useful. You can include both header files, and then call the functions like this:

MathPlus::Multiply();
or
MathLibrary::Divide();

If you want to use math plus''s math functions for the majority of your program with out refering to the namespace again you can put this under the include files

using namespace MathPlus;

Now you can call
Multiply() and it will be the same as calling MathPlus::Multiply, and you can still refrence MathLibraries divide function by doing MathLibrary::Divide();

I realize that the multiply and divide examples are a bit lame considering C++ has built in functionality for them already, but I thought it would be a good example of how and why you might use namespaces.




Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.

This topic is closed to new replies.

Advertisement