Domanda

Possible Duplicate:
Cast pointer to member function to normal pointer

there are some codes

class TT {
public:
  void set();
  void par1(int, int);
  void par2(double, double);
};

typedef void(*Ptr1)(TT &, int, int);

typedef void(*Ptr2)(TT &, double, double);

void hello(Ptr1, Ptr2){...}

void TT::set()
{
  hello(&TT::par1, &TT::par2);
}

and the error shows:

error C2664: 'hello' : cannot convert parameter 1 from 'void (__thiscall TT::* )(int,int)' to 'Ptr1'

please tell me how to solve this problem?

È stato utile?

Soluzione

You want to do this

typedef void(TT::*Ptr1)(int, int);
typedef void(TT::*Ptr2)(double, double);

And fix hello function to take a pointer to this since you need the this pointer to call a function on an object.

void hello(TT* obj,Ptr1 x, Ptr2 y){
    (obj->*x)(1,2);
}

Check this post out for more information on member function pointers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top