是否删除的指针的基类的工作? [英] Does delete work with pointers to base class?

查看:115
本文介绍了是否删除的指针的基类的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你必须通过删除由new返回相同的指针,也可以将指针传递它的类基本类型之一吗?例如:

Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types? For example:

class Base
{
public:
    virtual ~Base();
    ...
};

class IFoo
{
public:
    virtual ~IFoo() {}
    virtual void DoSomething() = 0;
};

class Bar : public Base, public IFoo
{
public:
    virtual ~Bar();
    void DoSomething();
    ...
};

Bar * pBar = new Bar;
IFoo * pFoo = pBar;
delete pFoo;

当然,这极大地简化了。我真正想要做的就是创建一个完整的boost :: shared_ptr的容器,并把它传递给一些code,当它完成后,将从容器中取出。这code将一无所知栏或基地的实施和将依靠隐含delete运算符中的shared_ptr析构函数做正确的事。

Of course this is greatly simplified. What I really want to do is create a container full of boost::shared_ptr and pass it to some code that will remove it from the container when it is finished. This code will know nothing of the implementation of Bar or Base, and will rely on the implied delete operator in the shared_ptr destructor to do the right thing.

可这可能工作呢?我的直觉说不准,因为指针不会有相同的地址。在另一方面,将dynamic_cast<酒吧*>应该工作,使地方编译器存储足够的信息来弄明白。



感谢您的帮助,大家谁回答和评论。我已经知道虚析构函数的重要性,如在我的例子;看到答案后,我给它一点思想,实现了全部原因的虚析构函数是这个确切的情况。因此它必须工作。我被缺席的指针转换回原始的可视装置抛出。多一点思考使我相信有一个看不见的手段,我推测,析构函数正在恢复的删除以释放真正的指针。调查从微软VC编译code ++证实了我的怀疑,当我看到这条线在〜基地:

Can this possibly work? My intuition says no, since the pointers will not have the same address. On the other hand, a dynamic_cast<Bar*> should work, so somewhere the compiler is storing enough information to figure it out.


Thanks for the help, everybody who answered and commented. I already knew the importance of virtual destructors, as shown in my example; after seeing the answer I gave it a little thought, and realized the whole reason for a virtual destructor is this exact scenario. Thus it had to work. I was thrown by the absence of a visible means of converting the pointer back to the original. A little more thinking led me to believe there was an invisible means, and I theorized that the destructor was returning the true pointer for delete to release. Investigating the compiled code from Microsoft VC++ confirmed my suspicion when I saw this line in ~Base:

mov	eax, DWORD PTR _this$[ebp]

跟踪汇编显示,这是被传递到删除功能的指针。解开了谜底。

Tracing the assembler revealed that this was the pointer being passed to the delete function. Mystery solved.

我已经修复添加虚拟析构函数的IFoo的例子,这是一个简单的监督。再次感谢大家谁指出了这一点。

I've fixed the example to add the virtual destructor to IFoo, it was a simple oversight. Thanks again to everyone who pointed it out.

推荐答案

是的,它会工作,当且仅当的基类的析构函数是虚拟的,这已为<$ C完成$ C>基础基类,但并非为的IFoo 基类。如果基类的析构函数是虚拟的,那么当你调用 delete操作符基类指针,它使用的是动态调度,通过查找弄清楚如何删除对象派生类析构函数中的虚函数表。

Yes, it will work, if and only if the base class destructor is virtual, which you have done for the Base base class but not for the IFoo base class. If the base class destructor is virtual, then when you call operator delete on the base class pointer, it uses dynamic dispatch to figure out how to delete the object by looking up the derived class destructor in the virtual function table.

在你的多重继承的情况下,它只会工作,如果你删除它通过基类有虚析构函数;它的确定对其他基础类没有虚析构函数,但前提是你不要试图通过与其他基类指针删除任何派生的对象。

In your case of multiple inheritance, it will only work if the base class you're deleting it through has a virtual destructor; it's ok for the other base classes to not have a virtual destructor, but only if you don't try to delete any derived objects via those other base class pointers.

这篇关于是否删除的指针的基类的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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