Why can't inner class and outer class have same name?

6407 views c++
13

My question is simple, why a class and its nested class cannot have same name, but namespaces can?

for example:

class Test {
    class Test {};
};

is invalid, but

namespace Test {
    namespace Test {}
}

is valid

answered question

Namespaces don't offer constructors. The name Test within class Test is reserved for that specific use.

1 Answer

3

Class X already has a member named X, referring to itself. This is known as an injected class name. It's then invalid to add another member with the same name.

posted this

Have an answer?

JD

Please login first before posting an answer.