March 18, 2021 question about ref keyword | ||||
---|---|---|---|---|
| ||||
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? also, when I do: m_s = s; m_s is a copy of s1 or a reference to it? 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? |
March 18, 2021 Re: question about ref keyword | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | 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;`. |
Copyright © 1999-2021 by the D Language Foundation