www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delegating constructor and call to super

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
class A {
	this(){...}
	this(int a) {...}
	this int a, int b){...}
}

class B {
	this(){... init some stuff for B ...}
	this(int a){super(a); this();}
}

Error: multiple constructor calls

I think it's because of "If a constructor's code contains a delegate 
constructor call, all possible execution paths through the constructor 
must make exactly one delegate constructor call"

But, how am I supposed to call the super(int) consturctor and put my 
common initialization code into a delegating constructor?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Jul 02 2018
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 2 July 2018 at 09:42:36 UTC, Robert M. Münch wrote:

 I think it's because of "If a constructor's code contains a 
 delegate constructor call, all possible execution paths through 
 the constructor must make exactly one delegate constructor call"

 But, how am I supposed to call the super(int) consturctor and 
 put my common initialization code into a delegating constructor?
class B { this(){ initialize(); } this(int a){super(a); initialize(); } private void initialize() { ... init some stuff for B ... } }
Jul 02 2018