A
Anonymous
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.
"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.