好的,所以我想看看通过它的成员变量删除一个类是否安全?成员变量有一个指向其包含类的指针。这在 m_objectPtr->OnDestroy(); 之后在 android 上崩溃;被调用。它适用于 IOS 同事告诉我。我想知道这段代码是否有效。
class Object;
class ValueObject;
class SomeManager;
class DataInterface
{
public:
virtual void OnDestroy(const ValueObject* value,void* data) = 0;
};
class destroyDelegate : public DataInterface
{
public:
destroyDelegate(Object* obj) : m_objectPtr(obj)
{
}
//...
virtual void OnDestroy(const ValueObject* value,void* data)
{
m_objectPtr->OnDestroy();
}
private:
Object* m_objectPtr;
};
class Object
{
public:
Object(SomeManager* manager,ValueObject* value) :
m_manager(manager), m_value(value), m_delegateObject(this)
{
// m_value calls OnDestroy when its being deleted. assume m_value is ref counted
m_value->setDestroyCallBack(&m_delegateObject);
}
void OnDestroy()
{
m_manager->Destroy(this); // deletes this class and removes it from its std::list
}
private:
SomeManager* m_manager;
ValueObject* m_value;
destroyDelegate m_delegateObject;
};
// now some where in code m_value dies and calls on its member variable that inherits
// DataInterface OnDestroy which chains down to deleting Object.