最近我一直在设计一个Thread类的库,我做了下面这样的主题抽象类:纯虚拟方法VS.函数指针
class Thread {
public:
run() { /*start the thread*/ }
kill() { /*stop the thread*/ }
protected:
virtual int doOperation(unsigned int, void *) = 0;
};
房地产线程类将继承这个抽象类,并在其自己的逻辑实现doOperation
方法,类似于Strategy Pattern。
的问题是,我依靠它定义运行的线程在下面的函数是C后端库:
int startThread(char* name, (int)(*)(unsigned int, void*), int, int, int, void*);
正如你所看到的;第二个参数是线程循环(主函数)的函数指针,这里是问题;由于我使用这个C函数来启动run
方法中的线程,因此我将doOperation
的地址传递给第二个参数,由于类型不匹配,无法完成此操作。
我试着用reinterpret_cast
来返回一个指针,但是我ISO-C++禁止返回一个未初始化函数成员的指针。 我不知道如何克服这种冲突,使用静态方法是我猜想的唯一解决方案,但它炸毁了我的设计模式!
Michael,Stefan,感谢您分享您的丰富想法,他们解决了我的问题。 – Josef 2009-02-02 00:29:58
请参见下面的问题: - http://stackoverflow.com/questions/499153/passing-a-qualified-non-static-member-function-as-a-function-pointer/499299#499299 – 2009-02-01 08:59:45