What is a namespace?
A namespace is basically a container for a group of variables/functions etc. I can pretty much guarantee everyone reading this tutorial has already used a namespace in C++ programming. Think of a line that usually comes at the top of a beginner program:
using namespace std;
This is the standard namespace. It's got the definitions of a lot of neat stuff in there.
Why should I use a namespace?
Namespaces are pretty neat. We use them to contain a group of data. Also, it makes it possible to have more than one variable with the same name. One of which is in the namespace, one which isn't. This will be explained later on.
When writing a namespace, there's nothing you need to include. A namespace is a standard container.
I'm gonna put my namespace in a seperate header file, just to save confusion.
Let's start with our header guards. I'm gonna call the header file "namespace.h" but you can call it whatever you like.
Also, I'm just gonna define it in the .h file. Granted, this is bad habit, but this is just a quick example.
#ifndef NAMESPACE_H_INCLUDED #define NAMESPACE_H_INCLUDED
Now we're ready to start writing a namespace. I'm gonna write a simple "math" namespace, since I've used something similar in a few posts to explain it already, and the people who I wrote it for found it quite useful.
There isn't too much to writing a namespace, but it's a useful skill. There isn't too much to break it down into, so I'm gonna post up a simple namespace here. This is namespace.h:
#ifndef NAMESPACE_H_INCLUDED
#define NAMESPACE_H_INCLUDED
namespace math // define a namespace called math
{
namespace numbers // an example of a nested namespace
{ // define some variables within our math::numbers namespace
int one = 1;
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;
int eight = 8;
int nine = 9;
} // end of nested namespace
// define some functions within our math namespace
int add (int x, int y) {return x + y;}
int subtract (int x, int y) {return x - y;}
int divide (int x, int y) {return x / y;}
int multiply (int x, int y) {return x * y;}
}
#endif // NAMESPACE_H_INCLUDED
As you can see, I've even made a nested namespace. You can nest namespaces as far as you like, but it seems a little pointless to have something as long as:
namespace1::namespace2::namespace3::namespace4::x just to access a variable named x.
And now that I've said that, you're probably thinking "what's this :: operator that this fool uses?"
The :: operator is known as the scope operator, and it is used to access certain things from within a namespace. The most common, which you may have seen before is something like:
std::cout << "something";
This line basically means that cout is defined in the std namespace. Makes sense, right?
Onwards to the various methods of accessing a namespace! This can be done with various methods.
There are three methods of accessing something from within a namespace. I'm gonna use the std namespace as an example here:
- using namespace std; This means that we want to be using everything which is defined within the std namespace
- using std::cout; This means that we only want to use cout from the std namespace
- This last method doesn't require the using keyword. We use it whenever we want to access something from the namespace. Example:
#include <iostream> // input/output stream int main () { std::cout << "Enter something!" << std::endl; char input; std::cin >> input; std::cout << "You entered: " << input << "!" << std::endl; std::cin.get (); // pause return EXIT_SUCCESS; }
Now for our main.cpp file, which is used to demonstrate everything we've learned so far:
#include <iostream>
#include "namespace.h"
// using a namespace
using namespace math; // using everything from the namespace called numbers
// (our nested namespace)
// using individual members of a namespace
using std::cout; // use cout from the std namespace specifically
using std::cin; // use cin from the std namespace specifically
using std::endl; // use endl from the std namespace specifically
int main ()
{
cout << math::add (numbers::three, numbers::two) << endl;
// note: if we weren't using the math namespace, we would have to prefix three and two with:
// math::numbers::three and math::numbers::two
// this is why it's kind of advisable against using huge nested namespaces!
// now, since we need to access two through numbers::two we can still have another variable called two
// because of the scope.
int two = 771123;
// now let's output both "two" variables and see what we get
cout << "two = " << two << endl
<< "numbers::two = " << numbers::two;
cin.get ();
return EXIT_SUCCESS;
}
Since it's so simple, I've decided a basic example added to the end of this tutorial would suffice. I have just written some basic code, which demonstrates the use of a nameless namespace. Here it is:
#include <iostream>
#include <string>
using std::string; // string datatype
using std::cout; // output to the console
using std::cin; // input from the console
namespace
{
string str = "hello"; // create a string, which is stored in our nameless namespaces
}
int main ()
{
// remember how we access things from within a namespace?
// we prefix it with the name of the namespace, then the scope operator
// since we don't have a name, all we need to do is prefix it with the
// scope operator, like so:
cout << ::str; // output the string
cin.get (); // hold the window open
return EXIT_SUCCESS; // successful execution
}
Told you it was simple!
Happy coding!









MultiQuote






|