How polymorphism work in D?
rikki cattermole
rikki at cattermole.co.nz
Wed Nov 6 06:05:25 UTC 2019
On 06/11/2019 6:43 PM, OiseuKodeur wrote:
> I have this
>
> ```
> import std.stdio : writeln;
>
> abstract class Foo { }
>
> class Bar : Foo
> {
> float value;
>
> this(float t_value) { value = t_value; }
> }
>
> class Baz : Foo
> {
> string name;
>
> this(string t_name) { name = t_name; }
> }
>
> void main()
> {
> Foo foo = new Bar(10);
>
> if (/* typeof foo == Bar */)
if (Bar bar = cast(Bar)foo)
bar.value.writeln;
> foo.value.writeln;
> else if (/* typeof foo == Baz */)
else if (Baz baz = cast(Baz)foo)
baz.name.writeln;
> foo.name.writeln;
>
> foo.writeln;
> }
> ```
>
> I don't understand how i can differentiate between Bar and Baz and to
> get their attributes
More information about the Digitalmars-d-learn
mailing list