Pass 'this' as reference

Steven Schveighoffer schveiguy at gmail.com
Wed Sep 12 15:11:57 UTC 2018


On 9/12/18 8:01 AM, Jan wrote:
> I'm using D not for that long and lately I have encountered an issue. I 
> have class 'Foo' with a constructor using this signature:
> `this (ref Bar original)`
> 
> In the 'Bar' class itself I want to create an instance of 'Foo' using 
> 'this' as parameter. Something in the way of:
> `Foo foo = new Foo(ref this);`
> 
> I couldn't find anything interesting on the internet to help me. Could 
> anyone help me? Many thanks in advance!

You don't have to specify ref when calling. This should work:

auto foo = new Foo(this);

Though almost certainly you are misunderstanding classes -- they are 
references anyway. I don't know why you would want to accept a class via 
ref unless you were actually going to reassign the reference. I suggest 
that your constructor should not accept Bar via ref.

-Steve


More information about the Digitalmars-d-learn mailing list