"this" as default parameter for a constructor.

Jack jckj33 at gmail.com
Mon Apr 12 18:21:04 UTC 2021


On Sunday, 11 April 2021 at 20:38:10 UTC, Pierre wrote:
> 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 ?

it isn't supported as far i know so use a default construtor like 
@Kagamin has show


More information about the Digitalmars-d-learn mailing list