By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Namespace: Is it a scope or a namespace?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.