recent

Please accept my apologies for the mess — the site is under construction.

previous

about / contact

the site
the webmaster

full cv (pdf)


github
form
linkedin
youtube


writing

the Lorentz boost (coming soon)







C++ | namespace
21:47 10-Jul-12

When coding, you'll find yourself using commands: commands such as those used to print text to screen, to display an image, to randomize a number, etc.. Also, you will likely find yourself creating and referencing variables.

In C++, the majority of these commands and variables are actually subsets of some greater command space (that's the way I like to think of it). The way that a program specifies the exact command he (or she) wishes to use is by explicitly stating from what namespace the command comes. It's almost like telling the compiler what language to be thinking in.

Think perhaps of the word cent. In English, it means one one-hundredth, whereas in French, it means one hundred. To remove the ambiguity we might say english::cent when we are referring to the 0.01 value and french::cent when we are referring to the 100 value. (Alternatively, we might say français::cent if we really wished to be accurate.) In these cases, "english" or "french" is the namespace.

The standard group of commands in C++ falls into the appropriately named "std" namespace. Thus, cout is actually std::cout; cin is actually std::cin; endl is actually std::endl; and so on. Of course, it becomes tiring to add std:: to the beginning of everything and is rather inefficient as well. Imagine if every time you wrote a paper, you were required to specify the language of each word before using it—english:it english:would english:not english:be english:worthwhile.

Instead of adding something like std:: before each command, instead you can specify a default namespace to work within by writing

using namespace std;

Effectively, this adds std:: before everything.

In addition to being able to work with existing namespace, you can define your own in the following way:

namespace SomeNameHere {
     int a = 1;
     int b = 4;
     string = "I exist in SomeNameHere";
}

By writing the above, you have effectively created a namespace named "SomeNameHere" and populated it with two integers and a string. To reference one of them, you would simply type SomeNameHere::a or SomeNameHere::string.


As a final note, if a namespace reference does not exist, it will be ignored. That is to say SomeNameHere::letter will revert to simply ::letter if letter does not exist within a SomeNameHere namespace.

Consider the following snippet of code:

namespace SomeNameHere {
     int a = 1;
     int b = 4;
     string = "I exist in SomeNameHere";
     char alphabeta = "j";
}
int a = 2;
int b = 1;
char alphabeta = "g";
char letter = "s";

std::cout << "A simple demonstration of namespaces\n\n";
std::cout << a << std::endl << b << std::endl;
std::cout << alphabeta << std::endl << SomeNameHere::alphabeta << std::endl;
std::cout << SomeNameHere::letter << std::endl << letter << std::endl;
std::cout << SomeNameHere::a << SomeNameHere::b;

Should produce the following output:

A simple demonstration of namespaces

2
1
g
j
s
s
1
4


Happy coding!!


This website is run and maintained by George Wong. This webpage was last modified 21:47 10-Jul-12 est.
You opened this page on
www.somewhereville.com/gnw and its subsidiaries are copyright 2003-2014.