inheritance and override conflict
Jarrett Billingsley
kb3ctd2 at yahoo.com
Thu May 25 15:29:51 PDT 2006
"GM" <GM_member at pathlink.com> wrote in message
news:e559gn$glk$1 at digitaldaemon.com...
>I started learning D to day. In playing around I found this behavior which
> seemed a little strange to me. As you can see below the Writer class has
> two
> prints. If I override one of them in WordWriter then I can no longer see
> the
> other without implicitly calling super. If I call writer.print("Yikes") in
> main
> as below compilation fails because Writer.print(char[]) is no longer
> visible.
Change the definition of WordWriter to the following:
class WordWriter : Writer
{
this()
{
writefln("go");
}
~this()
{
writefln("and");
}
void printWords(char[] str)
{
foreach(char c; str)
{
if(c == ' ')
super.print("\n");
else
print(c);
}
super.print("\n");
}
alias Writer.print print;
override void print(char o)
{
super.print(o);
}
}
Notice the "alias Writer.print print." This brings the "super" version of
the print method into the namespace of the WordWriter class, so that it
overloads with the other methods.
Also notice that if you were to put a colon after "override" it would make
it apply to all subsequent method (and member) declarations, which might not
be what you want. Notice in my version that it only affects the "void
print(char o)" declaration.
More information about the Digitalmars-d
mailing list