I don't get it. version(unittest) can't seem to use local variable
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 12 20:37:07 PDT 2014
On Sun, Jul 13, 2014 at 02:39:00AM +0000, dysmondad via Digitalmars-d-learn wrote:
> On Saturday, 12 July 2014 at 05:23:29 UTC, Ali Çehreli wrote:
> >On 07/11/2014 10:08 PM, dysmondad wrote:
> >
> >> class Velocity
> >> {
> >
> >[...]
> >
> >> ref Velocity opOpAssign(string op) ( in float multiplier
> >)
> >
> >Unrelated to your question, you want to return just Velocity there.
> >Unlike C++, classes are reference types in D. So, Velocity itself is
> >essentially a Velocity* in C++.
> >
> >Ali
>
> Right you are. I knew that but it still didn't translate into code.
> Thank you for pointing that out. I guess eventually I will remember
> that class variables are pointers.
>
> As a point of curiosity, is the ref keyword in this case simply
> redundant or does it actually make a difference?
ref makes it possible for the caller to modify the pointer returned by
the callee. For example:
class D { int x; }
class C {
D d;
this(D _d) { d = _d; }
ref D getPtr() { return d; }
}
auto d1 = new D;
auto d2 = new D;
auto c = new C(d1); // c.d now points to d1
assert(c.getPtr() is d1); // getPtr returns d1
c.getPtr() = d2; // this modifies c.d
assert(c.getPtr() is d2); // getPtr now returns d2
If you do not wish the caller to do this, remove the ref from the
function signature.
T
--
Don't drink and derive. Alcohol and algebra don't mix.
More information about the Digitalmars-d-learn
mailing list