inheritance and override conflict

GM GM_member at pathlink.com
Thu May 25 15:01:59 PDT 2006


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.



import std.stdio;

class Writer {
static this() {
x = 1;
y = 2;
z = 3;
}

this() {
writefln(x, y, z);
}

~this() {
writefln(z, y, x);	
}

void print(char[] o) {
writef(o);
}

abstract void print(char o) {
writef(o);
}
private:
static const int x, y, z;	
}

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");	
}

override:
void print(char o) {
super.print(o);
}	
}

void main() {
char[] str = "Hello, World!"; 
void delegate(char[]) f;

WordWriter writer = new WordWriter();
f = &writer.printWords;

writer.print("Yikes");

f(str);
}





More information about the Digitalmars-d mailing list