How can I call a method given only its name?

Posted by mfolnovich on Stack Overflow See other posts from Stack Overflow or by mfolnovich
Published on 2009-12-23T22:26:21Z Indexed on 2010/06/15 15:32 UTC
Read the original article Hit count: 143

Filed under:
|
|
|

I'm trying to have method void run( string method ) which would run method in that class. For example:

class Foo {
    public:
    void run( string method ) {
        // this method calls method *method* from this class
    }
    void bar() {
        printf( "Function bar\n" );
    }
    void foo2() {
        printf( "Function foo2\n" );
    }
}

Foo foo;

int main( void ) {
    foo.run( "bar" );
    foo.run( "foo2" );
}

this would print:

Function bar
Function foo2

Thanks! :)

© Stack Overflow or respective owner

Related posts about c++

Related posts about class