Tutorial: Namespaces
Namespaces prevent naming conflicts by putting functions in their own "zone".
To just use one function of a namespace, you could do this:
std::cout << "std is a namespace" << endl;
In the above line, std is a namespace.
Using Namespaces
But if you don't want to write "std::" infront of each item from the std namespace, you could do this:
using namespace std; cout << "std is a namespace" << endl;
You can 'use' two or more namespaces at the same time. For example :
using namespace gdn; using namespace std;
This is fine so long as there are no functions or classes that have the same name in both namespaces.
Declarations
You can make single functions of a namespace local (rather than the whole thing) by using a declaration :
using std::cout; using std::cin; using std::endl;
Creating a Namespace
To make your own namespaces, you can do this:
namespace position { int get_position(); void set_position(int x, int y); }
Extending Namespaces
You can include more functions in a namespace from a library, but it's advisable not to.
| Categories: Beginner Tutorials : Tutorials |
page_revision: 8, last_edited: 1246070910|%e %b %Y, %H:%M %Z (%O ago)