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

Viewed 145

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

1 Answers

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.

Related