Constructor inheritance? Why not?

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 8 02:20:05 PDT 2015


On Monday, 8 June 2015 at 06:35:33 UTC, Jonathan M Davis wrote:
> On Monday, 8 June 2015 at 06:04:23 UTC, Tofu Ninja wrote:
>> No its definitely not something that alot of people seem to 
>> care about, but I always found it odd that constructors get 
>> certain special casing like this.
>
> I expect that it originally comes down to the fact that 
> constructors aren't polymorphic and _can't_ be polymorphic.

They can if you use type variables. Type variables can be virtual 
too.

> When you override a function, you're overriding the one in the 
> base class and replacing its implementation. You can choose to 
> call the base class' version of the function, but you don't 
> have to. You're dynamically replacing a function.

Conceptually you should. In Simula's successor Beta you had to, 
but you had "inner" inheritance opposed to Simula/C++ "outer" 
inheritance. E.g. the logic of the subclass is executed by the 
superclass. It is much cleaner, actually.

> So, yes, constructors _are_ fundamentally different from other 
> functions. And the inheritance that you get with constructors 
> is not at all what you get when you override a function. 
> They're at most superficially similar. And older languages such 
> as C++98 and Java don't have inherited constructors. They're a 
> relatively new idea.

Let's not rewrite history, shall we? Constructors are not 
fundamentally different from other functions by nature. In 
minimal OO languages there is no significant difference between 
the definition of a class/constructor and a function.


More information about the Digitalmars-d mailing list