Passing a derived class where base class is defined as ref parameter
chopchop
bullshtttt at gmail.com
Mon Dec 13 22:06:45 UTC 2021
Hi guys,
below a small piece of code, which does not compile because "b"
of type B can not be passed as ref parameter to incr(ref A a).
If I remove the ref, it works as expected, that is to say I can
give a derived class as parameter. I have an idea why it does not
work, but I think a c++ reference would work, ie incr(A& console)
would accept a B as parameter. What the logic here?
Thanks
```
void main()
{
import std.stdio: write, writeln, writef, writefln;
class A
{
int i = 2;
}
class B : A
{
int j= 3;
}
void incr(ref A a)
{
writeln(a.i);
}
B b = new B();
incr(b);
}
```
More information about the Digitalmars-d-learn
mailing list