Class templates and constructors -
templates great add features class, there problem constructors: works when template ctor , class (passed parameter) ctor have default form. (dpaste tester)
module main; class cinternalmandatoryclass{}; class cimplementsomestuffs(t): t if((is(t==class)/* & (haveadefaultctor!t) */)) { private: cinternalmandatoryclass fobj; public: void something1(){} this(){fobj = new cinternalmandatoryclass;} ~this(){delete fobj;} } class csource1 { int fa; this(){fa = 8;} } class csource2 { int fa; this(){} this(int a){fa = a;} } class csourcewithsomestuffs1: cimplementsomestuffs!csource1 { this() { assert(fobj !is null); // check cimplementsomestuffs ctor assert(fa == 8); // check csource1 ctor } } class csourcewithsomestuffs2: cimplementsomestuffs!csource2 { this(int a) { // need call csource2 ctor assert(fobj !is null); // check cimplementsomestuffs ctor assert(fa == 9); // check csource2 ctor, fails } } void main(string[] args) { auto foo = new csourcewithsomestuffs1(); delete foo; auto bar = new csourcewithsomestuffs2(9); delete bar; }
is possible call csource2 ctor in csourcewithsomestuffs2 ? if not, there trait test class have default constructor ?
you can build chain of super
calls, forwarding constructor arguments:
in cimplementsomestuffs
:
this(a ...)(a args) // formerly this() { super(args); // etc ...
in csourcewithsomestuffs2
:
this(int a) // this(a ...)(args), { super(a); // etc ...
Comments
Post a Comment