Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ referring to a sibling namespace

Tags:

c++

namespaces

given:

namespace root { namespace parent { namespace childa
    class hard_to_get_at{};
}}}

namespace root { namespace parent { namespace childb
    // how do I refer refer to namespace childb relative to the current namespace ?
    ..::hard_to_get_at instance_of_childa_class; // psuedo syntax
}}}

Do I need to specify the full path of the namespace? Is there some way around it ?

like image 868
Hassan Syed Avatar asked May 20 '11 09:05

Hassan Syed


2 Answers

Next should work :

namespace root{
namespace parent{
namespace childb{

// some function where you want to use class hard_to_get_at
void foo()
{
   childa::hard_to_get_at obj;
   // do stuff
}

} // namespace childb
} // namespace parent
} // namespace root
like image 89
BЈовић Avatar answered Oct 11 '22 03:10

BЈовић


I have not tried it, but as far as I remember

childa::hard_to_get_at   sibling;

should work from within childb without the need for defining a namespace alias. this is a property of C++ namespace resolution, which is able to move up the hierarchy of nesting to search for namespaces.

like image 39
sergio Avatar answered Oct 11 '22 04:10

sergio