"this" as default parameter for a constructor.

Pierre demoulin.pierre at gmail.com
Sun Apr 11 20:38:10 UTC 2021


Hi,

I have a class with a reference to the parent object and a 
constructor that has the parent as parameter

class foo {
	this ( foo p /* , other params */ ) {
		parent = p;
	}

	foo parent;
}

Of cause, the parent is almost always the object that creates the 
new intance. So

auto f = new foo(this);

I'd like to set "this" ( the creator ) as default argument if the 
constructor :

this ( foo p = this ) {...}

I can't. But however, the function, the argument and the current 
object in body of the constructor are 3 different things and the 
compiler can distinguish each one.

Is there a way to pass the reference of the caller to the creator 
as default argument ?



More information about the Digitalmars-d-learn mailing list