Tuesday, 15 July 2014

c++ - Nested Namespace: accessing value from the inside namespace -



c++ - Nested Namespace: accessing value from the inside namespace -

i using nested namespace in c++ , trying access value of outer namespace within inner namespace , not working? can 1 please explain why?

code:

#include <iostream> using namespace std; namespace test { int x = 20; namespace intest { int x = 30 + x; } } int main() { using namespace test::intest; cout << "x = " << x << endl; homecoming 0; }

output:

x = 30

for above code snippet expecting value of x '50' '30'. can please explain doing wrong here?

os: windows, tool: codeblocks compiler: g++ (mingw)

int x = 30 + x;

x on right side of look refers test::intest::x because considered declared. add together 30 x own value, equal 0 @ startup because of zero-initialization of static objects (it takes place before other initialization according [basic.start.init]/2).

c++ namespaces scope

No comments:

Post a Comment