hafiz031
hafiz031

Reputation: 2670

Syntax of Constructor Defining

Why it is required to write class name with constructor name while defining a constructor outside the class?....does it make sense?..as the class name can be easily detected from the constructor's name as they must have the same name and as it has no return type we can differentiate it from other normal functions having same name as well.

Upvotes: 0

Views: 44

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595712

Why it is required to write class name with constructor name while defining a constructor outside the class?....does it make sense?

Yes. Same as when defining any other method of the class. The constructor is still a member of the class, and all member methods have to be scoped properly when defined outside of the class declaration. Imagine what would happen if multiple classes in different namespaces have the same name. Specifying the constructor name without scoping it by the namespace and class names would cause ambiguities.

Upvotes: 3

Related Questions