如果对象是使用新放置创建的多态类型,有没有办法在对象上调用析构函数?
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;
}
我只想通过指向其虚拟基础的指针来破坏“实施”……这可能吗?