Reputation: 2664
Hello I have the following structure in my code:
namespace ns
{
class A
{
public:
class Impl
{
public: static int x;
};
class B
{
public:
class Impl
{
public:
Impl(){printf("%d", ns::A::Impl::x);}
};
};
};
}
Is there a way to access that x property, using a relative, not the absolute path?
It would be more convenient, because, someday I could change the namespace name from ns
to e.g. other
, or put this entire file into some outer namespace.
Upvotes: 3
Views: 134
Reputation: 141658
There isn't an equivalent of "..", i.e. a way to specify "the enclosing namespace/class", but you can manually implement a similar thing:
class B
{
typedef A parent; // <---- add this
// ...
printf("%d", parent::Impl::x);
In this particular situation it doesn't gain anything: this is just a more roundabout way of having A::Impl::x
as Maxim suggested. But in general, if you have a more complicated hierarchy then this form may gain you some readability.
Upvotes: 2
Reputation: 136515
Is there a way to access that x property, using a relative, not the absolute path?
Symbols from the enclosing scope/namespace are accessible in the current scope/namespace.
ns::A::Impl::x
could be shortened to A::Impl::x
(can't omit A::
because you have two Impl
).
Upvotes: 4