jeudi 30 mars 2017

Convert object function to function pointer

I'm trying convert a object function to function pointer but can't get it, i've done something like this, simple example:

typedef struct
{
    int v1;

    int DoSome(int a)
    {
        return v1 * a;
    }
} strx;


int main()
{
    strx a; // instance...
    a.v1 = 2;


    std::function<int(strx* instance, int value)> DoSome = std::mem_fn(&strx::DoSome);

    cout << DoSome(&a, 4) << endl; // 16 ok 

    int(*pDoSome)(strx* instance, int value) = (int(*)(strx*, int))std::mem_fn(&strx::DoSome); // syntax error

    // ptr method...
    pDoSome(&a ,4);

    return 0;
} 

and i have obtained something like:

main.cpp [Error] invalid cast from type 'std::_Mem_fn' to type 'int ()(strx, int)'

How i can do correctly the casting?

Aucun commentaire:

Enregistrer un commentaire