toString OutputWriter method not calling derived class
rikki cattermole
rikki at cattermole.co.nz
Fri May 25 09:32:49 UTC 2018
On 25/05/2018 9:21 PM, cc wrote:
> When defining a toString(W)(ref W writer) function on a base class and
> derived class, only the version on the base class is called when
> formatting into a string. Is this intended behavior?
>
> import std.format;
> import std.range.primitives;
> class Foo {
> string test() {
> return "Foo?";
> }
> void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
> put(writer, "<FOO>");
> }
> }
> class Bar : Foo {
> override string test() {
> return "Bar?";
> }
> void toString(W)(ref W writer) if (isOutputRange!(W, char)) {
> put(writer, "<BAR>");
> }
> }
> Foo[] list = [new Foo(), new Bar()];
> foreach(i, item; list) {
> writefln("%d: %s (%s)", i, item, item.test());
> }
>
>
> Result:
> 0: <FOO> (Foo?)
> 1: <FOO> (Bar?)
>
Yes, because templates are not virtual.
More information about the Digitalmars-d-learn
mailing list