mercredi 26 août 2015

Dynamic function call C++

I would like to do something like this:

class Base{};
class Specialized1 : public Base
{
public:
    int GetCount(){ return 1; }
};
class Specialized2 : public Base
{
public:
    bool IsCorrect() { return true; }
};

class Example
{
public:
    template< class ATTR_CLASS, class RETURNED_PARAMETER_CLASS >
    int GetPerfectAttributeIndex( const RETURNED_PARAMETER_CLASS & perfect_parameter, ***RETURNED_PARAMETER_CLASS (*function_to_call)()*** )
    {
        for ( int i = 0; i < AttributeCount; ++i )
        {
            if ( perfect_parameter ==
                static_cast< ATTR_CLASS >( MyAttributeTable[ i ] )->function_to_call() )
            {
                return i;
            }
        }
        return -1;
    }


    Base** MyAttributeTable;
    int AttributeCount;
};

And the call would be:

example.GetPerfectAttributeIndex< Specialized1, int >( 1, &Specialized1::GetCount );

So I know that this code is not working because of the part between *** But how can I change it to make it work? Using some C++11 magic?

Thank you for any help!

Aucun commentaire:

Enregistrer un commentaire