Wings Of Fire

Namespace Concept in c++

Posted in:  Academics Saturday 30th, January 2010
 
Views(3)  
Rajesh  U
student

Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:


namespace identifier
{
entities
}


Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example:

namespace myNamespace

{

int a, b;

}



In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For example, to access the previous variables from outside myNamespace we can write:

myNamespace::a

myNamespace::b



The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

// namespaces

#include <iostream>

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;

}

5

3.1416



In this case, there are two global variables with the same name: var. One is defined within the namespace first and the other one in second. No redefinition errors happen thanks to namespaces.

using

The keyword using is used to introduce a name from a namespace into the current declarative region. For example:

// using

#include <iostream>

using namespace std;

namespace first

{

int x = 5;

int y = 10;

}

namespace second

{

double x = 3.1416;

double y = 2.7183;

}

int main () {

using first::x;

using second::y;

cout << x << endl;

cout << y << endl;

cout << first::y << endl;

cout << second::x << endl;

return 0;

}

5

2.7183

10

3.1416



Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

// using

#include <iostream>

using namespace std;

namespace first

{

int x = 5;

int y = 10;

}

namespace second

{

double x = 3.1416;

double y = 2.7183;

}

int main () {

using namespace first;

cout << x << endl;

cout << y << endl;

cout << second::x << endl;

cout << second::y << endl;

return 0;

}

5

10

3.1416

2.7183



In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:

// using namespace example

#include <iostream>

using namespace std;

namespace first

{

int x = 5;

}

namespace second

{

double x = 3.1416;

}

int main () {

{

using namespace first;

cout << x << endl;

}

{

using namespace second;

cout << x << endl;

}

return 0;

}

5

3.1416

Write your comment now


*Email    *Password: 
Don't have SiliconIndia account? Sign up    Forgot your password? Reset

  Cancel
Reader's comments(1)
1:Actually, the namespaces were introduced in c++ because many of the functions began to conflict with those defined globally. For e.g. if a function named abs() is defined, it gets conflicted by the globally defiend one.. Here namespaces allow users to make their own functions to be used locally.
Posted by: Vaibhav Pandey - 02nd Feb 2010
Disclaimer
Messages posted on this Web site under the `Comments' area are solely the opinions of those who have posted them and do not necessarily reflect the opinions of Infoconnect Web Technologies India Pvt Ltd or its site www.siliconindia.com. Gossip, mud slinging and malicious attacks on individuals and organizations are strictly prohibited. Infoconnect Web Technologies India Pvt Ltd can not be held responsible for errors or omissions in content, nor for the authenticity of the user/company name or email addresses associated with posted messages. Infoconnect Web Technologies India Pvt Ltd reserves the right to edit or remove messages containing inappropriate language or any other material that could be construed as libelous, potentially libelous, or otherwise offensive or inappropriate.Infoconnect Web Technologies India Pvt Ltd do not endorse the products and services or any other offerings mentioned in these messages.
Blogs archive
More Academics blogs
FAT (File Allocation Table) is a series of file systems, de...more >>
To ease the load on faculty members taking admission interviews for t...more >>
Most of the people choose their career after considering the money ma...more >>
SCHOOL EDUCATION IN RURAL INDIA According to the Annual Status of Edu...more >>
· Can you tell us about JIM, Noida? Jaip...more >>

CXO Insights

Souma Das
Souma Das
Area Vice President - India Sub Continent, Citrix Systems India
Stephanie Moore
Stephanie Moore
CMO, UST Global Inc
Narayana Murthy
Narayana Murthy
Chairman & Chief Mentor, Infosys Technologi es.
Sid Agrawal
Sid Agrawal
CEO and Director, SiPort.
News:         Technology   |   Enterprise IT   |   Tech Products   |   Startups   |   Finance   |  Business   |  Career   |  Magazine   |  Dailydose   |   News archive    |  
RSS
Network:      Network   |   Profile   |   Messages   |   Scrapbook   |   Find   |   Blogs   |   Communities   |   Events   |   Q&A   |   CXO Insights
Career:       Jobs   |   Companies   |   Test your skills   |   Mentorship   |   Videos   |   Career blogs  |   Training institutions  |   Freshers
Job training courses:   Web developer   |  Java developer   |  CCNA training
Education:   MBA   |  MCA   |  Engineering |  US  | Internship
Life:                  Humor   |   Bookstore   |   Relocate  |  Marketplace  
Cities:            Startup  |  Real estate  |  Finance
Company:  About us   |   Contact   |   Help   |   Community rules   |   Advertise with us
Member directory:   A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z 
Send your feedback and help us continue to improve SiliconIndia
© 2008 SiliconIndia all rights reserved