为什么虚拟基地dtor被调用? [英] why virtual base dtor gets called?
问题描述
基本的虚拟dtor被隐式调用是否正确?
这里有一些代码来证明我的意思:
B类有一个虚拟的析构函数因此,D类是从B中获得的,B
来自D的dtor和
然后是B的dtor。
我我认为这只适用于非虚拟dtor的情况,
但是我不希望它发生在一个虚拟的dtor上。
对于一个班级来说一个虚拟的dtor我原本预计只有当D被删除时才会调用D的dtor.
什么是正确的?可能是我的编译器车吗?
B级
{
公共:
B() {}
virtual~B(){std :: cout<< "〜B\\\
英寸; }
};
D级:公共B
{
public:
D(){}
virtual~D(){std :: cout<< "〜D\\\
英寸; }
};
void virtual_dtor_tester()
{
B b;
D d;
}
/ *输出:
~D
~B
~B
* /
Is it correct that the virtual dtor of base gets called implicitly?
Here''s some code to demonstrate what I mean:
Class B has a virtual destructor, so has class D which
is derived from B. Deleting D calls the dtor of D and
then the dtor of B.
I was thinking that this would be true only for non-virtual dtor case,
but I wouldn''t have expected it happen for a virtual dtor.
For a class with a virtual dtor I would have expected that only
the dtor of D would be called when D gets deleted.
What''s correct? Is maybe my compiler buggy?
class B
{
public:
B() {}
virtual ~B() { std::cout << "~B\n"; }
};
class D : public B
{
public:
D() {}
virtual ~D() { std::cout << "~D\n"; }
};
void virtual_dtor_tester()
{
B b;
D d;
}
/* output:
~D
~B
~B
*/
推荐答案
>对于具有虚拟dtor的类,我原本预计只有
> For a class with a virtual dtor I would have expected that only
当D被删除时,将调用D的dtor。
the dtor of D would be called when D gets deleted.
Nope 。由于D是B,D的基本部分必须被销毁,因此~B()被称为
。
无论B或D是否包含,总是如此虚拟功能。
编译器是正确的。
Stephen Howe
Nope. Since D is a B, the base part of D must be destroyed, hence ~B() is
called.
That is always true regardless of whether B or D contains virtual functions.
Compiler is correct.
Stephen Howe
" tuvok" < 52 *************** @ t-online.de>在消息中写道
news:d8 ************* @ news.t-online.com ...
"tuvok" <52***************@t-online.de> wrote in message
news:d8*************@news.t-online.com...
这是正确的吗base的虚拟dtor是否被隐式调用?
不是真的,基类的虚拟参数被调用,而不是调用。
这里有一些代码来证明我的意思:< B类有一个虚拟析构函数,所以D类是从B派生的.D删除D调用D的dtor然后是B.的命令。
B'd'tor在D''sd可以完成之前被调用和处理。
我以为这只适用于非虚拟dtor情况,
对于一个有虚拟dtor的课程,我原本预计只有当D被删除时才会调用D的dtor。
您正在描述非虚拟目标。只有D''sd~tor才会被调用。
这就是为什么衍生类需要基础的争论的关键因为
有虚拟目标。
什么是对的?可能是我的编译器车吗?
B级
{
公开:
B(){}
虚拟~B(){std :: cout << "〜B\\\
英寸; } D />};
D类:公共B
{
公开:
D(){}
虚拟~D(){ std :: cout<< "〜D\\\
英寸; }
};
void virtual_dtor_tester()
{b>;
}
> / *输出:
~D
~B
~B
* /
Is it correct that the virtual dtor of base gets called implicitly?
Not really, the virtual d~tor of base class is invoked, not called.
Here''s some code to demonstrate what I mean:
Class B has a virtual destructor, so has class D which
is derived from B. Deleting D calls the dtor of D and
then the dtor of B.
B''s d~tor is invoked and processed before D''s d~tor can complete.
I was thinking that this would be true only for non-virtual dtor case,
but I wouldn''t have expected it happen for a virtual dtor.
For a class with a virtual dtor I would have expected that only
the dtor of D would be called when D gets deleted.
You are describing a non-virtual d~tor. Only D''s d~tor would be invoke.
Which is the crux of the arguement of why a derived class needs the base to
have the virtual d~tor.
What''s correct? Is maybe my compiler buggy?
class B
{
public:
B() {}
virtual ~B() { std::cout << "~B\n"; }
};
class D : public B
{
public:
D() {}
virtual ~D() { std::cout << "~D\n"; }
};
void virtual_dtor_tester()
{
B b;
D d;
}
/* output:
~D
~B
~B
*/
尝试:
#include< iostream>
B级
{
public:
B(){std :: cout<< " B\\\
英寸; }
virtual~B(){std :: cout<< "〜B\\\
英寸; }
};
D级:公共B
{
public:
D(){std :: cout<< " D\\\
英寸; }
~D(){std :: cout<< "〜D\\\
英寸; }
};
class E:public D
{
public:
E(){std :: cout<< " E\\\
英寸; }
~E(){std :: cout<< "〜E\\\
英寸; }
};
int main(int argc,char * argv [])
{
E e ;
返回0;
}
/ *
B
D
E
~E
~D
~B
* /
注意:d~torler~D()和~E()是自动虚拟的。
Try:
#include <iostream>
class B
{
public:
B() { std::cout << "B\n"; }
virtual ~B() { std::cout << "~B\n"; }
};
class D : public B
{
public:
D() { std::cout << "D\n"; }
~D() { std::cout << "~D\n"; }
};
class E : public D
{
public:
E() { std::cout << "E\n"; }
~E() { std::cout << "~E\n"; }
};
int main(int argc, char* argv[])
{
E e;
return 0;
}
/*
B
D
E
~E
~D
~B
*/
Note: d~tors ~D() and ~E() are automatically virtual.
Peter Julian写道:
Peter Julian wrote:
tuvok写道:
对于有虚拟dtor的课程,我原本预计只有D的dtor才会当D被删除时被调用。
你正在描述一个非虚拟的数据。只有D'的调用才会被调用。关于为什么派生类需要基础来拥有虚拟目标的争论的关键在哪里。
For a class with a virtual dtor I would have expected that only
the dtor of D would be called when D gets deleted.
You are describing a non-virtual d~tor. Only D''s d~tor would be
invoke. Which is the crux of the arguement of why a derived class
needs the base to have the virtual d~tor.
请不要回答问题在这里,如果你不知道你在谈论什么
。调用派生类的析构函数
总是调用基类析构函数。
基类析构函数的虚拟性只进入
通过指向基座的指针删除
类。
尝试:
#include< iostream>
B班
{
公开:
B(){std :: cout<< " B\\\
英寸;虚拟~B(){std :: cout<< "〜B\\\
英寸; } D />};
D类:公共B
{
公开:
D(){std :: cout<< " D\\\
英寸; }
~D(){std :: cout<< "〜D\\\
英寸; } E />};
E类:公共D
公开:
E(){std :: cout<< " E\\\
英寸; }
~E(){std :: cout<< "〜E\\\
英寸; }
};
int main(int argc,char * argv [])
{
返回0;
}
/ *
B
D
~E
~D
~B
* /
Please don''t answer questions here if you don''t know what
you are talking about. Invoking a derived class''s destructor
ALWAYS calls the base class destructors.
The virtualness of the base class destructor only comes into
play when the object is deleted via a pointer to the base
class.
Try:
#include <iostream>
class B
{
public:
B() { std::cout << "B\n"; }
virtual ~B() { std::cout << "~B\n"; }
};
class D : public B
{
public:
D() { std::cout << "D\n"; }
~D() { std::cout << "~D\n"; }
};
class E : public D
{
public:
E() { std::cout << "E\n"; }
~E() { std::cout << "~E\n"; }
};
int main(int argc, char* argv[])
{
E e;
return 0;
}
/*
B
D
E
~E
~D
~B
*/
即使B'的dtor不是虚拟的,输出也是一样的。
试试看看。
The output will be the same even if B''s dtor is not virtual.
Try it and see.
这篇关于为什么虚拟基地dtor被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!