Passing and returning arguments by ref

Joe jma at fc.com
Fri Mar 3 14:03:47 UTC 2023


Let's say we have two classes, A and B.  The latter has a dynamic 
array of X and type X has an add() method that can be used to 
append elements (of type C, another struct) to X's own dynamic 
array of C.  So it's something like the following:

```d
struct C {}
struct X { C[] cs;
   void add(C c) { cs ~= c; }
}

class A {
   X x;
   void mfd(ref B b) {
      // manipulate A.x and one B.xs (returned by B.x1()
      // by calling another member function
      // that calls X.add(some C)
   }
}

class B {
   X[] xs;
   this() { xs.length = 1; }
   ref X x1() { return xs[0]; }
}
```

After A and B are instantiated and A.mfd() is called, the changes 
to A.x are visible in main.  However, although the changes to the 
X returned by B.x1() are visible while A.mfd() executes, the 
B.xs[0] is unchanged after it returns.

My understanding was that since A, B and X[] are all reference 
types, this ought to work, but obviously something is missing.


More information about the Digitalmars-d-learn mailing list