0% found this document useful (0 votes)
12 views20 pages

4 Namespaces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

4 Namespaces

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

C++ namespaces


New concept introduced in C++

Declarative region that provides a scope to the identifiers (the names of
types, functions, variables, etc) inside it

Used to organize code into logical groups and to prevent name collisions
(e.g. when code includes multiple libraries)

Scope resolution operator (::) can be used to access variables in other
scopes

using keyword can be used to introduce specific members of a
namespace in to the current scope or all members of the namespace in
to the scope of common parent

Namespaces can be nested
C++ namespaces
#include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
int main()
{
fun();
nspace::fun();
cout << x << endl;
cout << nspace::x << endl;
return 0;
}
C++ namespaces
#include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
int main()
{
//fun(); // Invalid
nspace::fun();
//cout << x << endl; // Invalid
cout << nspace::x << endl;
return 0;
}
C++ namespaces
#include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
using nspace::fun; //using declaration
int main()
{
fun();
nspace::fun();
cout << x << endl;
cout << nspace::x << endl;
return 0;
}
C++ namespaces
#include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
using nspace::fun; //using declaration
int main()
{
fun();
nspace::fun();
//cout << x << endl; // Invalid
cout << nspace::x << endl;
return 0;
}
C++ namespaces
#include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
using namespace nspace; //using directive
int main()
{
fun();
nspace::fun();
cout << x << endl;
cout << nspace::x << endl;
return 0;
}
C++ namespaces
#include<iostream>
using namespace std;
int x = 10;
namespace nspace
{
int x = 20;
void fun()
{
int x = 30;
cout << x << " " << nspace::x << " " << ::x << endl;
}
}
int main()
{
nspace::fun();
return 0;
}
C++ namespaces

A namespace can be split into multiple blocks and those blocks can be in
the same file or separate files.

One file can contain blocks of multiple namespaces
1.cpp 2.cpp
namespace abc namespace xyz
{ {
... ...
} }

namespace xyz namespace abc


{ {
... ...
} }

namespace abc namespace xyz


{ {
... ...
} }
C++ namespaces (using declaration)
#include<iostream>
int x = 1;
namespace nspace
{
int x = 2;
int y = 3;
}
int main()
{
int y = 4;
using nspace::y;
using nspace::x;
std::cout << x << " " << y << “ “;
int fun();
fun();
return 0;
}
int fun()
{
std::cout << x;
}


Here nspace::x has been introduced within main function scope
C++ namespaces (using declaration)
#include<iostream>
int x = 1;
namespace nspace
{
int x = 2;
int y = 3;
}
int main()
{
int y = 4;
//error: ‘y’ is already declared in this scope
//using nspace::y;
using nspace::x;
std::cout << x << " " << y << “ ”;
int fun();
fun();
return 0;
}
int fun()
{
std::cout << x;
}

Here nspace::x has been introduced within main function scope
C++ namespaces (using declaration)
#include<iostream>

namespace test {
int x = 7;
}

namespace test2 {
using test::x;
}

int main() {
std::cout << test2::x;
return 0;
}


Output?
C++ namespaces (using declaration)
#include<iostream>

namespace test {
int x = 7;
}

namespace test2 {
using test::x;
}

int main() {
std::cout << test2::x;
return 0;
}


Output

7
C++ namespaces (using directive)
#include<iostream> int main()
{
int x = 1; int y = 5;
using namespace nspace;
namespace nspace std::cout << " " << x;
{ std::cout << " " << ::x;
int x = 2; std::cout << " " << y;
int y = 3; std::cout << " " << ::y;
int z = 4; std::cout << " " << z;
} std::cout << " " << ::z;
void fun();
fun();
return 0;
}
void fun()
{
std::cout << " " << x;
}
C++ namespaces (using directive)
int main() 
nspace::x is qualified name of x.
#include<iostream>
{

Unqualified name is a name that does not
int y = 5; appear to the right of a scope resolution
int x = 1; operator ::
using namespace nspace;
//error: reference to ‘x’ is ambiguous

x is unqualified name of variable x.
namespace nspace
{ //std::cout << " " << x; 
For unqualified name lookup form main function
int x = 2; std::cout << " " << ::x;
(after using directive statement) it is as if all
int y = 3; std::cout << " " << y; members of the namespace nspace are part of
int z = 4; //error: ‘::y’ has not been declared global scope
} //std::cout << " " << ::y; 
Produces error only if we try to access x within
std::cout << " " << z; main function (after using directive statement)

Qualified name lookup //error: ‘::z’ has not been declared  Unqualified name lookup for x starts from main
for ::x, ::y and ::z, starts //std::cout << " " << ::z; (local scope), but x is not present in main hence it
from global scope, and void fun(); checks global scope where it find more than one
only ::x is considered fun(); declaration of x hence the error
as part of global scope. return 0; 
Unqualified name lookup for y starts from main
naspace::y and } (local scope), and y is present in main hence
naspace::z are not void fun() search stops there and it uses local y from main
considered part of 
Unqualified name lookup for z starts from main
{
global scope for (local scope), but z is not present in main hence it
std::cout << " " << x;
qualified name lookup. checks global scope where it find one declaration
}
of z.
C++ namespaces (using directive)
#include<iostream> using namespace nspace;
int main()
int x = 1; {
int y = 5;
namespace nspace std::cout << " " << x;
{ std::cout << " " << ::x;
int x = 2; std::cout << " " << y;
int y = 3; std::cout << " " << ::y;
int z = 4; std::cout << " " << z;
} std::cout << " " << ::z;
void fun();
fun();
return 0;
}
void fun()
{
std::cout << " " << x;
}
C++ namespaces (using directive)
#include<iostream> using namespace nspace; 
Qualified name lookup for ::x, ::y and ::z,
int main() starts from global scope, and only ::x is
int x = 1; { considered as part of global scope.
int y = 5; naspace::y and naspace::z are not
namespace nspace //error: reference to ‘x’ is ambiguous considered part of global scope for
{ //std::cout << " " << x; qualified name lookup.
int x = 2; std::cout << " " << ::x; 
In this case using directive statement is
int y = 3; std::cout << " " << y; present in global scope. So when y and z
int z = 4; std::cout << " " << ::y; are not found in global scope, search
} std::cout << " " << z; continues within namespaces for which
std::cout << " " << ::z; using directive is present in global
void fun(); scope (nspace in this case). And hence
fun(); search for ::y and ::z ends with nspace::y
return 0; and nspace::z respectively.
}
void fun() 
As using directive statement is present in
{ global scope now, for unqualified name
//error: reference to ‘x’ is ambiguous lookup after using directive statement,
//std::cout << " " << x; it is as if all members of the namespace
} nspace are part of global scope

Hence unqualified name lookup for x from
function fun results in ambiguity.
C++ namespaces (using directive)
#include<iostream>

namespace test {
int x = 7;
}

namespace test2 {
using namespace test;
}

int main() {
std::cout << test2::x;
return 0;
}


Output?
C++ namespaces (using directive)
#include<iostream> 
Qualified name lookup for test2::x starts
from test2. So when x is not found in test2,
namespace test {
search continues within namespaces for
int x = 7;
} which using directive is present in test2
scope (test namespace in this case). And
namespace test2 { hence search for test2::x ends with test::x.
using namespace test;
}

int main() {
std::cout << test2::x;
return 0;
}


Output

7
C++ namespaces (using directive)
#include<iostream>
int x = 1;
namespace test {
namespace test2 {
int x = 2;
}
namespace test3 {
using namespace test2;
void fun() {
std::cout << x;
std::cout << test::x;
std::cout << " " << ::x;
}
}
}
int main() {
test::test3::fun();
return 0;
}


Output?
C++ namespaces (using directive)
#include<iostream> 
Qualified name lookup for test::x starts from test namespace. So when
int x = 1; x is not found in test namespace, search continues within namespaces
namespace test {
for which using directive is present in test namespace (there is no
namespace test2 {
int x = 2; using directive directly within test). Hence search stops there without
} finding x, and hence compiler generates an error.
namespace test3 {
using namespace test2; 
Qualified name lookup for ::x starts directly from global scope and x is
void fun() { present in global scope.
std::cout << x;
//error: ‘x’ is not a member of ‘test’ 
For unqualified lookup for x, it searches in local scope (fun function
//std::cout << test::x;
scope) first. Variable x is not present in fun function and hence it will
std::cout << " " << ::x;
} look into parent scope of fun scope (test3). x is not present in test3.
} Hence search continues to its parent (test). Variable x is found in test.
} Variable x is considered part of test (for unqualified name lookup)
int main() { because of using directive in test3.
test::test3::fun();
return 0;
}


Output

21

You might also like