user13739935
user13739935

Reputation:

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

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

Upvotes: 3

Views: 306

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52601

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.

Upvotes: 2

Related Questions