typedefs cannot be used to call superclass members

anders at runesson.info anders at runesson.info
Sat Jul 15 08:14:59 PDT 2006


Hi there. 
I have 2 classes and two typedefs like so:

file events.d: 
********************
class BEvent {
..
public uint Time() { return time; }
}

class BMouseEvent: BEvent {
..
public int X() { return x; }
public int Y() { return y; }
}

typedef BMouseEvent BMousePressedEvent;
typedef BMouseEvent BMouseReleasedEvent;
***************************

and in my main method I have this:

main.d
**********
..
bool handleEvent(BEvent e) {
BMousePressedEvent ev = cast(BMousePressedEvent) e;

//BMouseEvent ev = cast(BMouseEvent) e; <-- this works
writefln("Event handler called, on ", ev.X, ".", ev.Y, " at ",
ev.Time);                  
return true;
}
***************

When I cast the reference to handleEvent to the typedef
BMousePressedEvent I get the compiler error:

main.d(99): this for X needs to be type BMouseEvent not type
BMousePressedEvent
main.d(99): this for Y needs to be type BMouseEvent not type
BMousePressedEvent
main.d(99): this for Time needs to be type BEvent not type
BMousePressedEvent

The docs say that typedefs can be implicitly converted to their
underlying type, but that doesn't seem to happen. Casting to the
underlying type manually(like the comment does) makes it work.
Any explanation? Am I doing it wrong, or is this a bug?

/Anders






More information about the Digitalmars-d-learn mailing list