{ 使用 Visual Studio 2010 , Win7 }
class Base
{
public:
Base() : terminateCondition(false)
{
//spawn new thread and set entry point to newThreadFunc()
}
virtual ~Base() // edited to say it's virtual.
{
terminateCondition=true;
//wait for thread to join
}
virtual void vfunc() = 0;
static void __stdcall newThreadFunc(void *args)
{
while(!terminateCondition)
pThis->vfunc();
}
volatile bool terminateCondition;
};
class Derived : public Base
{
public:
virtual void vfunc()
{
//Do Something
}
};
Derived* dPtr=new Derived; //now assume pThis is dptr
//later somewhere
delete dPtr;
此代码崩溃说pure virtual called
. 移动terminateCondition=true
到析构函数Derived
可以防止这种崩溃。我想我部分明白了为什么。破坏与构造的顺序相反,因此首先执行 d'tor ,并且在调用 d'tor 之前破坏Derived
的所有功能。同时,如果遇到应用程序将崩溃。它崩溃说纯虚拟调用。我无法理解这部分。有人可以解释一下吗?Derived
Base
pThis->vfunc()