We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,466 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Shared and Separate name space

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

3
Contributors
2
Replies
19 Minutes
Discussion Span
4 Months Ago
Last Updated
3
Views
klint.austero
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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 {};
}
deceptikon
Challenge Accepted
Administrator
3,459 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,159 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2982 seconds using 2.68MB