C++. Calling a virtual member function in destructor [duplicate]

六眼飞鱼酱① 提交于 2019-12-24 11:43:50

问题


Every class that gets extended with this calls abort in the destructor and the call stack tells me that the function that called abort was called from a unreasonable spot in the header file. The other functions get overriden and work fine though.

Renderable.h

#pragma once
class Renderable
{
public:
    virtual void update(long delta) = 0;
    virtual void destroy() = 0;
    virtual void render() = 0;
    virtual ~Renderable();
};

Renderable.cpp

#include "Renderable.h"

Renderable::~Renderable()
{
    (this->*(&Renderable::destroy))(); // GLUtils::runOnUIThreadWithContext(this, &Renderable::destroy, true);
} // It says that it gets called from here.


回答1:


When instantiating an object the base class gets initialized and then the subclass gets initialized. When destructing an object the subclass gets destructed and then the base class. After the subclass is destructed, its members and virtual methods are unavailable—there is no destroy() method to be invoked. I suggest you move the logic in the destroy() method to the subclass destructor.



来源:https://stackoverflow.com/questions/32910882/c-calling-a-virtual-member-function-in-destructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!