1,105,363 Community Members

Shared and Separate name space

Member Avatar
klint.austero
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What and how does shared namespace in C++ works? Are most of the variables, types, and procedures can be used in shared name space?

Member Avatar
deceptikon
Eternally Awesome
4,691 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

Namespace definitions can be split across multiple files and still have the same name, is that what you mean by "shared"?

// file1.cpp
namespace foo {
    class A {};
    class B {};
}

// file2.cpp
namespace foo {
    class C {};
    class D {};
}

When the project is built, those two namespaces are merged because they have the same name, so concerning membership in the namespace it's as if you wrote it like this:

namespace foo {
    class A {};
    class B {};
    class C {};
    class D {};
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,641 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

No such thing as "shared" namespaces. A namespace is a namespace, it doesn't have "shared" attribute. A namespace is just something that better organizes the data, classes, structures and code. It's main purpose it to avoid name clashes. Without namespaces all names must be unique throughout the program (with a few exceptions). If I declared a variable named int x; in one *.cpp file and declared a variable with the same name in another *.cpp file the liker would most likely complain that it found two variables with the same name. But if I put each of those variables in different namespaces then the linker would have no problem because each would actually be different variables.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article