陪伴而非守候
还有执行回调的C方法:函数指针//Define a type for the callback signature,//it is not necessary, but makes life easier//Function pointer called CallbackType that takes a float//and returns an inttypedef int (*CallbackType)(float); void DoWork(CallbackType callback){ float variable = 0.0f; //Do calculations //Call the callback with the variable, and retrieve the //result int result = callback(variable); //Do something with the result}int SomeCallback(float variable){ int result; //Interpret variable return result;}int main(int argc, char ** argv){ //Pass in SomeCallback to the DoWork DoWork(&SomeCallback);}现在,如果您希望将类方法作为回调传递,则这些函数指针的声明具有更复杂的声明,例如://Declaration:typedef int (ClassName::*CallbackType)(float);//This method performs work using an object instancevoid DoWorkObject(CallbackType callback){ //Class instance to invoke it through ClassName objectInstance; //Invocation int result = (objectInstance.*callback)(1.0f);}//This method performs work using an object pointervoid DoWorkPointer(CallbackType callback){ //Class pointer to invoke it through ClassName * pointerInstance; //Invocation int result = (pointerInstance->*callback)(1.0f);}int main(int argc, char ** argv){ //Pass in SomeCallback to the DoWork DoWorkObject(&ClassName::Method); DoWorkPointer(&ClassName::Method);}