Passing a derived class where base class is defined as ref parameter

Adam D Ruppe destructionator at gmail.com
Mon Dec 13 22:30:59 UTC 2021


On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote:
> If I remove the ref, it works as expected, that is to say I can 
> give a derived class as parameter.

Why are you using the ref to begin with?

> What the logic here?

Consider this:


class C : A {}

void incr(ref A a) {
    a = new C;
}

B b = new B;
incr(b);
// oops b now got rebound to a C instead of to a B, which breaks 
everything


More information about the Digitalmars-d-learn mailing list