如果对象是使用放置new创建的多态类型,是否有方法可以调用析构函数?多态放置东西上的析构函数
class AbstractBase{
public:
~virtual AbstractBase(){}
virtual void doSomething()=0;
};
class Implementation:public virtual AbstractBase{
public:
virtual void doSomething()override{
std::cout<<"hello!"<<std::endl;
}
};
int main(){
char array[sizeof(Implementation)];
Implementation * imp = new (&array[0]) Implementation();
AbstractBase * base = imp; //downcast
//then how can I call destructor of Implementation from
//base? (I know I can't delete base, I just want to call its destructor)
return 0;
}
我想仅仅通过指向它的虚拟基地来破坏“实施”......这可能吗?
'base->〜AbstractBase()',或'imp->〜Implementation()'。你选。 – Casey
这甚至不是有效的C++。你可以'删除基地',这将调用派生的虚拟析构函数。你确定你想在这里使用虚拟继承吗? – Aesthete
是的,你是对的! http://ideone.com/Dv4JpO谢谢! Esthete从凯西的解决方案是足够的:)。 – GameDeveloper