www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D - Runtime Polymorphism Issue

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 ?
Apr 03 2007