我想制作一个线程对象,成员函数“run”可以被覆盖。当我添加“虚拟”一词时,它会失败。有人可以帮我吗 - 我怎样才能制作一个线程对象。对象可以继承,成员函数可以重写。
#include <iostream>
#include <process.h>
using namespace std;
class thread
{
private:
static void gangplank(void *ptr)
{
((thread *)ptr)->run();
}
public:
void start()
{
_beginthread(&this->gangplank,0,(void *)this);
//this->gangplank((void *)this);
}
virtual void run()
{
cout<<1;
}
~thread()
{
_endthread();
}
};
class d:public thread
{
public:
void run()
{
cout<<2;
}
};
int main()
{
d a;
a.start();
return 0;
}
错误信息:
"text.exe Has stopped working - Windows is checking for a solution to the problem"
没有编译错误
最佳答案
我不知道这是不是你的问题,因为你只是这么说
失败了,没有说如何,但你不等待线程
在 main
中完成,因此您可能正在破坏线程对象
在线程开始运行之前。
关于c++ - 类虚拟成员函数中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443435/