On Mon, 17 May 2010 16:38:49 -0400, Larry Luther <larry.lut...@dolby.com> wrote:

Hi, Dan used the following method for his vector class:

  void opOpAssign (string op:"+=")(ref Vector3 other) {...}

Why the "ref"?  As I understand it, objects of class vector
would already be passed as references.

  Larry

Yes, ref is redundant in this case. However, you can ref a class reference, meaning you can change the passed-in class reference.

An example:

class C
{
  int x;
}

void foo(ref C xyz)
{
   xyz.x = 5
   xyz = new C;
   xyz.x = 6;
}

void bar(C xyz)
{
   xyz.x = 7;
   xyz = new C;
   xyz.x = 8;
}

void baz()
{
   C c = new C;
   c.x = 1;
   C c2 = c;
   foo(c);
   // foo changed the x member of the object c and c2 pointed to, and then
   // actually changed the object c points to.
   assert(c.x == 6 && c2.x == 5 && c !is c2);

   c2 = c;
   c.x = 1;
   bar(c);

   // bar changed the x member of the object c and c2 pointed to, and then
   // created a local C which did not affect the class reference passed in.
   assert(c.x == 7 && c2.x == 7 && c is c2);
}

-Steve

Reply via email to