digitalmars.D.learn - 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?
Mar 18 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?rightalso, when I do: m_s = s; m_s is a copy of s1 or a reference to it?a copysetting 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;`.
Mar 18 2021








ag0aep6g <anonymous example.com>