ctor not inhrited in a classes

Basile B. b2.temp at gmx.com
Sun Jun 16 20:43:05 UTC 2019


On Sunday, 16 June 2019 at 14:11:15 UTC, Zaher Dirkey wrote:
> I discussed this on IRC before years but i didnt get good 
> answer.
>
> Let assume I have library that used by my coworkers
>
> In the example bellow my coworkers used SubBaseClass to create 
> their classes, in first version of my code I dont have ctor 
> with arg, and everything is fine,
> but later i added parent to it and i want SubBaseClass and 
> (child
>  classes) not created without parent value, i want to force 
> them to do that.
>
> what happen the new ctor not inhrited to child classes, and 
> coworkers still can create it using (new) without parameters, 
> we have alot of child classes,
> I want compiler stop compiling if parent not defined.
>
>
> import std.stdio;
>
> class SuperClass {
>     this(){
>
>     }
> }
>
>
> class SubBaseClass: SuperClass {
>
>     this(){
>
>     }
>
>     SubBaseClass parent;
>
>     this(SubBaseClass Parent) {
>       //some code to use parent etc
>       writeln("childclass ctor");
>     }
> }
>
> //CoWorkers or who are using my lib
>
> class ChildBaseClass1: SubBaseClass {
> }
>
> class ChildBaseClass2: SubBaseClass {
> }
>
>
> void main()
> {
> 	//compiled
>     ChildBaseClass1 a = new ChildBaseClass1();
>     ChildBaseClass2 b = new ChildBaseClass2();
>
>     //error:is not callable using argument types
>     //but i need to force to use to init varables
>     ChildBaseClass1 a = new ChildBaseClass1(null);
>     ChildBaseClass2 b = new ChildBaseClass2(a);
>
> }

There was a DIP proposing better inhertence of constructors. 
Unfortunately it's been dropped : 
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1004.md
Meaning that someone need to replace the original author(s).

Maybe something based on mixin can help in your case, even if 
that's not clean IMO.



More information about the Digitalmars-d mailing list