c++ - Namespace Scope Question -


i have quick question namespace scope:

  1. i have 2 namespaces, , b, b nested inside a.
  2. i declare typedefs inside a.
  3. i declare class inside b ( inside )

to access typedefs (declared in a), inside b, need "using namespace a;"

ie:

b.hpp:

using namespace a;  namespace { namespace b {    class myclass {      typedeffed_int_from_a a;   };  } } 

this seems redundant... correct?

to access typedefs (declared in a), inside b, need "using namespace a;"

no.


however if there typedef or other symbol same name typedef, defined in namespace b, need write this:

a::some_type a; 

lets simple experiment understand this.

consider code: (must read comments)

namespace {    typedef int some_type; //some_type int     namespace b    {         typedef char* some_type;  //here some_type char*          struct x         {                some_type     m; //what type of m? char* or int?                a::some_type  n; //what type of n? char* or int?         };          void f(int) { cout << "f(int)" << endl; }         void f(char*) { cout << "f(char*)" << endl; }    } }  int main() {         a::b::x x;         a::b::f(x.m);         a::b::f(x.n);         return 0; } 

output:

f(char*) f(int) 

that proves type of m char* , type of n int expected or intended.

online demo : http://ideone.com/abxc8


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -