"this" as default parameter for a constructor.

Pierre demoulin.pierre at gmail.com
Tue Apr 13 15:57:24 UTC 2021


On Monday, 12 April 2021 at 13:14:27 UTC, Kagamin wrote:
> class foo {
> 	this ( foo p /* , other params */ ) {
> 		parent = p;
> 	}
> 	foo create() {
> 		return new foo(this);
> 	}
> 	void use() {
> 		foo f = create();
> 	}
>
> 	foo parent;
> }

It's a solution. But my foo class is a base for several 
subclasses which each have their own constructor with different 
parameters; i should define as much create function as subclasses 
constructors. that's unelegant. I tried do solve it with 
templates but couldn't found a satisfatory solution.

As @Jack say " this ( T p = this ){...} " if not supported, not 
yet, maybe it should be ?

By now, the "best" way i found is to write auto f = new foo(this);


More information about the Digitalmars-d-learn mailing list