April 03, 2007
I am experiencing polymorphism problems while implementing a visitor pattern.
I've got a visitor interface :
        interface Visitor
    {
        char [] visit ( T node );
    }
and several subclasses of class Node.
Node has got a "char [] accept ( Visitor visitor )" method, that I override in its subclasses:
    override char [] accept ( Visitor visitor )
    {
        return visitor.visit ( this );
    }
I am trying to generate some C code with a VisitorC object that implements Visitor:
    char [] content = "int main ( int * argc, char ** argv );" ;
    auto analysis = analyse ( content ); // Of type FunctionType ( char [] T = "Function" ): Node casted to Node.
    auto visitor = new VisitorC ;

    cout << typeid ( typeof ( analysis )) << \n ; // Prints compile time type: Node.
    cout << "code gen: " << analysis.accept ( visitor ) << \n ;
( cout is an instance of a class with a templated opShl method that uses writef )
I was expecting to get the visit ( FunctionType ) method called, but the overload resolution is made according to the compile time type, not the runtime type and the Node.visite () method is called instead.

I wanted to know if this was a feature, or if I missed something in order to get the behaviour I want. Has anyone  a clue about this ?

Top | Discussion index | About this forum | D home