xmllmx
xmllmx

Reputation: 42335

How to hide `A::x` and expose `B::x` only in such a case?

namespace A
{
int a = 1;
int x = 2;
}

namespace B
{
int b = 3;
int x = 4;
}

using namespace A;
using namespace B;
using B::x;

int main()
{   
    return x; // error : reference to 'x' is ambiguous
}

How to hide A::x and expose B::x only in such a case?

Upvotes: 0

Views: 50

Answers (2)

Bathsheba
Bathsheba

Reputation: 234715

using namespace brings that namespace into the current one.

Once you've done that you need to deal with any ambiguities yourself. Normally you use the scope resolution operator for that.

C++ doesn't give you the ability to un-bring in a namespace.

The best bet, by a country mile, is to avoid using namespace in the first place. Learn to love code containing lots of :: operators.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You can't.

You brought both names into scope, and that's that.

To fix that, don't do that; avoid using namespace.

Upvotes: 2

Related Questions