The C++ namespace: the 'using' directive C++ code example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Separating the different namespaces using the namespace 'using' directive in C++ programming

To show: How to have different namespaces in C++ programming using the namespace 'using' directive




// the using directive

#include <iostream>

using namespace std;


namespace One


double p = 3.1234;



namespace Two


using namespace One;


double q = 4.5678;


namespace InTwo


double r = 5.1234;




int main(void)


// this using directive gives you all which declared in Two

using namespace Two;


// this using directive gives you only the InTwo namespace

using namespace Two::InTwo;


// this is local declaration, it takes precedence

// to see the different, comment this code and re-run this program

double p = 6.12345;


cout<<"p = "<<p<<endl;

cout<<"q = "<<q<<endl;

cout<<"r = "<<r<<endl;


return 0;



Output example:


p = 6.12345

q = 4.5678

r = 5.1234

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index