The code below compiles (sorry about that again) and is actually the code problem I originally wanted to post. Now play the compiler – which is Bar’s base class and why:
namespace NS { namespace NS { class Cls // Candidate #1 { } } class Cls // Candidate #2 { } namespace Foo { class Bar : NS.Cls // Who am I? { } } }
For extra credit, how do I force the compiler to choose the other one?
[Update: Good going, everyone! Candidate #2 is indeed the one used. To use candidate #1, you must either apply the ‘global::’ prefix or explicitly use the class’s full name.]
Candidate #1
Choose candidate #1 by inheriting just Cls
Just a guess, I haven’t tested it.
Who am I? NS.NS.Cls
To access NS.Cls
global::NS.Cls
Is candidate #2
To be number 1 it had to be
class Bar : NS.NS.Cls
{
}
2 — namespace hierarchy will take precedence
Saifee is correct
The :: operator is the namespace alias qualifier operator where global:: would start at the global namespace
You could do
using doubleNs = NS.NS;
class Bar : doubleNs::Cls // Who am I?
{
}
to alias the NS.NS namepace
It Will take candidate 1, since the class is already under the namespace NS and when u write ns.cls u are reffering to the the class inside the name space ns in side the current name space.
To get the other class use the base class as cls.
Vikram