What is the sense in giving names to constructors

abstraction, c++, constructor, java

Solution

From the C++ standard (12.1) (emphasis mine):

Constructors do not have names. A special declarator syntax is used to declare or define the constructor. The syntax uses:

- an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,

- the constructor’s class name, and

- a parameter list

In C++, you are not providing a name, you are writing special syntax which was decided by the language creators to declare a constructor.

Problem

I have been using C++ and Java for several years now. One thing which I can't seem to understand is that why do we need to provide constructors of a class a name? For instance, if I have to define a class `FOO` in C++/Java, I'll be forced to provide `FOO` as the constructor name. However, since constructor is never explicitly called, what is the sense in compiler forcing me to provide it a name after all. The abstraction paradigm dictates, we hide unnecessary details from programmers. This is the reason, constructors don't have a return type, since it's already well-defined what a constructor has to return. In the same spirit, why can't we just give a generic name to constructors of all classes - for instance anything meaningful, like `initialize()` or maybe just nothing and just arguments `( [arg [,arg]] )` I hope, I'm able to express myself. If someone have any definitive answers, kindly let me know.

Original source