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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With