question about ref keyword

ag0aep6g anonymous at example.com
Thu Mar 18 17:46:49 UTC 2021


On Thursday, 18 March 2021 at 16:59:24 UTC, Jack wrote:
> let's assume this class:
>
> class C
> {
> 	private S m_s;
>
> 	this()
> 	{
> 		m_s = S(30);
> 	}
>
> 	ref S value() { return m_s; }
> 	ref S value(ref S s)
> 	{
> 		return m_s = s;
> 	}
> }
>
> and I do something like this:
>
> 	auto s1 = S(40);
> 	auto c = new C();
>         c.value = s1;
> 	s1.n = 80;
>
>
> give that value has ref in its signature, the s1 is passed as a 
> "pointer" right?

right

> also, when I do: m_s = s; m_s is a copy of s1 or a reference to 
> it?

a copy

> setting s1.n to 80 in the next line doesn't seem to change 
> c.value so it seems it passed s1 as a pointer but when it comes 
> to assignment a copy was made?

You got it.

If you want the pointer, you can get it taking the address of s: 
`S* p = &s;`.


More information about the Digitalmars-d-learn mailing list