473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Namespace: Is it a scope or a namespace?

Is namespace the same thing as scope? While reading the book
"Thinking in C++", I was under the impression that namespace is, well,
a namespace--a feature to create a hiearchy for identifiers within the
Global Namespace. And that, all identifiers within a given namespace,
however deeply nested they are, each of them has the Global Scope.

So, if

namespace outer {
int a = 1;
namespace inner {
int b = 2;
void myprint () { cout << a;}
//can I do this, and will it print 1?
}

then both outer::a and outer::inner:b variables will have global
scope, and both variables can be accessed from all the other
scopes--function, class, and block scopes--provided they are not
overridden within the other scope. Can someone help clarify the
difference between namespace and scope for me? I'm confused.
Jul 19 '05 #1
3 6434
Anonymous wrote:
Is namespace the same thing as scope? While reading the book
"Thinking in C++", I was under the impression that namespace is, well,
a namespace--a feature to create a hiearchy for identifiers within the
Global Namespace. And that, all identifiers within a given namespace,
however deeply nested they are, each of them has the Global Scope.

So, if

namespace outer {
int a = 1;
namespace inner {
int b = 2;
void myprint () { cout << a;}
//can I do this, and will it print 1?
}

then both outer::a and outer::inner:b variables will have global
scope, and both variables can be accessed from all the other
scopes--function, class, and block scopes--provided they are not
overridden within the other scope. Can someone help clarify the
difference between namespace and scope for me? I'm confused.


A suggestion; experiment. It takes a minute to get this to compile and run.

Do you think you could write some code that would test your theory ?

G

Jul 19 '05 #2
"Anonymous" <no@spam.net> wrote in message
news:q3********************************@4ax.com
Is namespace the same thing as scope? While reading the book
"Thinking in C++", I was under the impression that namespace is, well,
a namespace--a feature to create a hiearchy for identifiers within the
Global Namespace. And that, all identifiers within a given namespace,
however deeply nested they are, each of them has the Global Scope.

So, if

namespace outer {
int a = 1;
namespace inner {
int b = 2;
void myprint () { cout << a;}
//can I do this, and will it print 1?
}
Yes.
then both outer::a and outer::inner:b variables will have global
scope, and both variables can be accessed from all the other
scopes--function, class, and block scopes--provided they are not
overridden within the other scope. Can someone help clarify the
difference between namespace and scope for me? I'm confused.


You seem to basically have it right. For the most part, a namespace just
creates naming rules for variables. The one case in which a variable
declared in a namespace is not accessible elsewhere is when an unnamed
namespace is used, e.g.,

namespace
{
int a, b;
// other stuff
}

In this case, it is not possible to refer to a or b outside the namespace.

The word "scope" is used with more than one meaning. One sense is that
destructors are called when control passes outside of a scope. Namespaces
are *not* scopes in that sense.

Another sense is the way in which "scope" is used when referring to "class
scope". A variable x or function foo with class scope can be referred to
directly as x or foo, respectively, by all member functions of the class.
Outside of the class, x and foo can only be referred to via class objects or
(in the case of static members) by using the class name --- and then only if
the caller has the required access privileges. Namespaces behave similarly
and in this sense do define a scope. That is why :: is called the "scope
resolution operator". A namespace is actually somewhat similar to a class in
which all data and functions are both public and static.
--

John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 19 '05 #3

"John Carson" <do***********@datafast.net.au> wrote in message news:3f**********@news.brisbane.pipenetworks.com.. .
"Anonymous" <no@spam.net> wrote in message
The word "scope" is used with more than one meaning.
As far as the standard is concerend there is one meaning. Scope refers to
the locality of name definitions.
One sense is that
destructors are called when control passes outside of a scope. Namespaces
are *not* scopes in that sense.


Because that's really a proper use of the term scope. It just happens that
the compound statement happens to be both a scope and a block of automatic
storage duration.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JustSomeGuy | last post by:
I am writing classes and I want them to belong to mynamespace What is the syntax to say that the class I'm defining is a member of mynamespace? What is the scope of the syntax and how does one...
3
by: CHRISTOF WARLICH | last post by:
Hi, the following few lines of code are showing a quite strange and unexpected behaviour of namespaces that makes me worry wheater I should rely on namespaces in the future at all. The...
9
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. ...
1
by: toton | last post by:
Hi, Is it possible to have a class level namespace opening instead of file level . Something like this, I have a long namespace like namespace com { namespace my_company{ namespace...
3
by: toton | last post by:
Hi, Is it possible to have a class level namespace opening instead of file level . Something like this, I have a long namespace like namespace com { namespace my_company{ namespace...
9
by: vthomasset | last post by:
Hi, Sorry for the bad subject, but i couldn't figure a better one. I would like to understand why a variable declared non static in a header causes multiple symbol definitions (if included in...
7
by: Juha Nieminen | last post by:
This is possible: namespace X { class A; } class X::A { <implementation}; However, what about nameless namespaces? Does this do what I want? namespace { class A; }
3
by: mackenzie | last post by:
I was wondering why it is "ill-formed if an allocation function is declared in a namespace"? I have done some searching on the web and can not find a reason why; I have stumbled across a few other...
3
by: CrazyJohn | last post by:
Hi guys, This is my first time posting question here, if I break any rules, please kindly point out. And I'm really glad to be a part of this community. Here is my question, Our lecturer...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.