Friday, May 18, 2007

namespace

namespace - declares a block in which other identifiers may be declared. An identifier declared within a namespace becomes a "sub-identifier" linked to the surrounding namespace identifier. It creates a names scope.

This is a good web page on namespace: http://www.cplusplus.com/doc/tutorial/namespaces.html

namespace myNamespace
{
int a, b;
}


myNamespace::a
myNamespace::b

#include
using namespace std;

namespace first
{
int var = 5;
}

namespace second
{
double var = 3.1416;
}

int main()
{
cout << first::var << endl;
cout << second::var << endl;
return 0;
}

Using the 'using' keyword the namespace can be accessed.

namespace first
{
int x = 5;
}

using first::x;

using namespace first;

cout << x << endl;

both of the above using lines would make the x variable available.

No comments: