对象应该在C ++中删除自己吗? [英] Should objects delete themselves in C++?

查看:136
本文介绍了对象应该在C ++中删除自己吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中工作了4年,所以我对C ++中的最佳实践和常见设计模式感兴趣。考虑以下部分示例:

  class World 
{
public:
void Add Object * object);
void Remove(Object * object);
void Update();
}

class Fire:Object
{
public:
virtual void Update()
{
if ; burnTime)
{
world.Remove(this);
delete this;
}
}
}

用于管理一组对象并定期更新它们。火是一种可能在许多不同情况下被添加到世界中的对象,但通常是已经在世界中的另一个对象。火是唯一知道什么时候被烧坏的对象,所以目前我有它自己删除。创建火的对象可能不再存在或相关。



这是一个明智的事情,还是有一个更好的设计,将有助于清理这些对象?

解决方案

这样做的问题是你真正创建了一个对象和World类之间的隐式耦合。 p>

如果我尝试在World类外调用Update(),会发生什么?我可能最终与被删除的对象,我不知道为什么。似乎责任混乱了。这将导致问题,当你使用Fire类在一个新的情况下,你没有想到当你写这个代码。如果对象应该从多个地方删除会发生什么?也许应该从世界,当前地图和玩家的库存中删除?您的更新功能将从世界上删除它,然后删除对象,下一次地图或库存尝试访问对象时,坏事发生。



一般来说,我想说,一个Update()函数删除它正在更新的对象是非常不直观的。我也说,对于一个对象来删除它自己是不直观的。
对象应该更可能有某种方式来激活一个事件,说它已经完成了刻录,任何有兴趣的人现在都可以执行了。例如通过从世界中删除它。要删除它,请考虑所有权。



谁拥有该对象?世界?这意味着世界独自决定对象何时死亡。这很好,只要世界的对象的引用将超过其他引用。
你认为对象本身吗?这是什么意思?当对象不再存在时,应该删除对象?



但是如果没有明确定义的单个所有者,则实现共享所有权,例如使用实现引用计数的智能指针,例如 boost :: shared_ptr



但是对象本身有一个成员函数,一个特定列表,其是否存在于其中,以及其是否也存在于任何其它列表中,并且还删除对象本身,而不管其对哪个引用存在,是不好的主意。

I've spent the last 4 years in C# so I'm interested in current best practices and common design patterns in C++. Consider the following partial example:

class World
{
public:
    void Add(Object *object);
    void Remove(Object *object);
    void Update();
}

class Fire : Object
{
public:
    virtual void Update()
    {
        if(age > burnTime)
        {
            world.Remove(this);
            delete this;
        }
    }
}

Here we have a world responsible for managing a set of objects and updating them regularly. Fire is an an object that might be added to the world under many different circumstances but typically by another object already in the world. Fire is the only object that knows when it has burned out so currently I have it deleting itself. The object that created the fire is likely no longer in existence or relevant.

Is this a sensible thing to do or is there a better design that would help clean up these objects?

解决方案

The problem with this is that you're really creating an implicit coupling between the object and the World class.

If I try to call Update() outside the World class, what happens? I might end up with the object being deleted, and I don't know why. It seems the responsibilities are badly mixed up. This is going to cause problems the moment you use the Fire class in a new situation you hadn't thought of when you wrote this code. What happens if the object should be deleted from more than one place? Perhaps it should be removed both from the world, the current map, and the player's inventory? Your Update function will remove it from the world, and then delete the object, and the next time the map or the inventory tries to access the object, Bad Things Happen.

In general, I'd say it is very unintuitive for an Update() function to delete the object it is updating. I'd also say it's unintuitive for an object to delete itself. The object should more likely have some kind of way to fire an event saying that it has finished burning, and anyone interested can now act on that. For example by removing it from the world. For deleting it, think in terms of ownership.

Who owns the object? The world? That means the world alone gets to decide when the object dies. That's fine as long as the world's reference to the object is going to outlast an other references to it. Do you think the object own itself? What does that even mean? The object should be deleted when the object no longer exists? Doesn't make sense.

But if there is no clearly defined single owner, implement shared ownership, for example using a smart pointer implementing reference counting, such as boost::shared_ptr

But having a member function on the object itself, which is hardcoded to remove the object from one specific list, whether or not it exists there, and whether or not it also exists in any other list, and also delete the object itself regardless of which references to it exist, is a bad idea.

这篇关于对象应该在C ++中删除自己吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆