我知道,为了在对象上创建方法的线程,我可以通过以下方式做到这一点:
#include <thread>
using namespace std;
class Character
{
public:
void myFunction(int a){ /* */ }
void startThreadMyFunction(int a){
thread Mf1(&Character::myFunction, this, a);
}
};
我也知道,为了在我的类中有一个指向函数的指针,我可以这样来做:
#include <thread>
using namespace std;
class Character
{
private:
void (*FMoveForward)(int);// Pointer to a function.
public:
void setCommands(void(mf)(int delay)){//This function sets the pointer.
FMoveForward = mf;
}
void MoveForward(int delay){
FMoveForward(delay);// Here a call my function with my pointer to function.
}
};
我的问题是,当我尝试同时使用这两种功能时,Visual Studio 13编译器始终会抱怨sintaxe。
#include <iostream>
using namespace std;
class Character
{
private:
void (*FMoveForward)(int);
public:
void setCommands(void(mf)(int delay)){
FMoveForward = mf;
}
void MoveForward(int delay){
thread Mf1(&Character::FMoveForward , this, delay);// The VS 13 Complain because the sintaxe os this line.
}
};
有人知道如何解决吗?TY进阶...
问题在于指向成员函数的指针不是指向自由函数的指针。std::thread
可以同时使用两者,但是您需要保持一致。
在第一个示例中,您有一个指向成员函数的指针。好的。
在第二个示例中,您有一个指向自由函数的指针。还行。
在您的第三个示例中,FMoveForward
是指向自由函数的指针。&Character::FMoveForward
是指向指针的指针。那是行不通的。
如果要存储&Character::myFunction
,则需要一个void (Character::*FMoveForward)(int);
成员。那是一个指向成员函数的指针
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句