Jump to content
  • Advertisement


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


whats namespace?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

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:


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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!