Reputation: 419
I have a following structure of namespaces
namespace a::b::c
{
class xyz{
};
using x=xyz
}
If I want to refer to x in an outside file, How should I address it? is it like a::b::c::x
or just x
?
To be able to refer to it as x
outside my code, how can I change the above code? Can I write using x=a::b::xyz
inside the namespace a::b::c?
Upvotes: 2
Views: 929
Reputation: 3
Creating a namespace in the first place is for the purpose of avoiding variable-name collisions. When you use a using
directive or create a global namespace alias, you're defeating the purpose of having used namespaces in the first place and re-establishing the danger of creating such a collision in order to avoid extra typing. Instead, use copy-paste editing or simply create a custom snippet in your IDE. (Or just type it out.)
Upvotes: 0
Reputation: 311186
The name x
is introduced in the namespace a::b::c
namespace a::b::c
{
class xyz{
};
using x=xyz
}
So in the global namespace to refer to the name x
you have to write
a::b::c::x
You could introduce the alias in the global namespace like
using x = a::b::c::xyz;
Alternatively you could use a using declaration like
using a::b::c::x;
in this case you can use the unqualified name x
in the scope where the using declaration is placed.
Upvotes: 2
Reputation: 1110
Okay, you need to understand that namespace
means a new name space in order to distinguish between possibly similar names.
When you use: namespace a::b::c
you add to all of the names inside it the prefix a::b::c
, therefore, you must call it using a::b::c::x
as you mentioned (and you can easily check it on your own!). Only inside of that namespace, you can use it as x
.
You can use it in other places by including it into the namespace, by putting using a::b::c::x
, but I don't understand why you want to do such a thing.
If you want to use it everywhere, just put the using x = a::b::c::x
outside of the namespace.
But, namespaces are created with a good purpose, please avoid removing the namespace prefix by doing any of these, it is just a bad habit.
Upvotes: 1