关于默认析构函数的问题...... [英] question about default destructors...

查看:102
本文介绍了关于默认析构函数的问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


默认的析构函数是虚拟的吗?


换句话说,说我有A类和B级:公共A,而我​​

的代码如下:


A * a =新A;

B * b =新B;

a = b;

删除a;


A或B都没有定义析构函数(因此,当删除时,为每个人创建一个默认的

析构函数)。被称为,它会先调用b'的

析构函数然后调用析构函数,还是只调用's $

析构函数?

Hello,

Are default destructors virtual?

In other words, say I have "class A" and "class B : public A", and I
have the code below:

A * a = new A;
B * b = new B;
a = b;
delete a;

Neither A or B have destructors defined (thus creating a default
destructor for each), when "delete a" is called, will it call b''s
destructor first then a''s destructor, or will it just call a''s
destructor?

推荐答案

6月13日晚上10点35分,Avalon1178< Avalon1 ... @ gmail.comwrote:
On Jun 13, 10:35 pm, Avalon1178 <Avalon1...@gmail.comwrote:

您好,


默认析构函数是虚拟的吗?
Hello,

Are default destructors virtual?



不,他们不是。理由是你不应该支付你不使用的
功能,并且上课不需要虚拟

析构函数。

No, they are not. The rationale is that you should not have to pay for
features you don''t use, and sone classes do not need virtual
destructors.




" Avalon1178"写道:

"Avalon1178" wrote:

默认析构函数是虚拟的吗?
Are default destructors virtual?



不。就在昨晚,我的编译器(gcc)警告我:

警告:类具有虚拟功能但非虚拟

析构函数。

如果您的基类

具有虚函数,并且您的派生类有数据,那么您真的只需要一个虚拟析构函数

需要以不同方式销毁的成员而不是

基类。

Nope. Just last night, my compiler (gcc) warned me:
"Warning: class has virtual functions but non-virtual
destructor".

You really only need a virtual destructor if your base class
has virtual functions, and if your derived classes have data
members that need to be destructed differently than than the
base class.


换句话说,我说有A类。和B级:公共A,

我的代码如下:


A * a =新A;

B * b =新B;

a = b;

删除a;


A或B都没有定义析构函数(因此,当删除时,为每个人创建一个默认的

析构函数)。被调用,它会先调用b'的
析构函数然后调用析构函数,还是只调用's $

析构函数?
In other words, say I have "class A" and "class B : public A",
and I have the code below:

A * a = new A;
B * b = new B;
a = b;
delete a;

Neither A or B have destructors defined (thus creating a default
destructor for each), when "delete a" is called, will it call b''s
destructor first then a''s destructor, or will it just call a''s
destructor?



这将调用A'的析构函数。如果B动态分配了数据成员,那么A就不会造成内存泄漏。

在这样的情况下,你应该放入这一行答:


A级

{

公开:

A();

虚拟~A();

...

私人:

...

};


然后让B'的析构函数做它需要做的任何事情。 (这是

为新'数据成员打电话'删除'。)


B级:公众A

{

public:

B(){Blat = new double;}

~B(){delete Blat;}

...

私人:

double * Blat;

...

};


然后如果你在main()中这样做:


int main(无效)

{

A * Fred =新B;

删除Fred; //调用~B(),不是~A()

返回42;

}


Fred将正确获取破坏了。


-

干杯,

Robbie Hatley

孤独的狼aatt well dott com

triple-dubya dott Tustin Free Zone dott org

That will call A''s destructor. If B has dynamically-allocated
data members which A doesn''t, that will create a memory leak.
In a case like that, you should put this line in A:

class A
{
public:
A();
virtual ~A();
...
private:
...
};

Then make B''s destructor do whatever it needs to do. (Such
as calling "delete" for new''d data members.)

class B : public A
{
public:
B() {Blat = new double;}
~B() {delete Blat;}
...
private:
double *Blat;
...
};

Then if you do this in main():

int main (void)
{
A* Fred = new B;
delete Fred; // calls ~B(), not ~A()
return 42;
}

Fred will then get correctly destructed.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org


int main(void)
int main (void)

{

A * Fred =新B;

删除Fred; //调用~B(),而不是~A()
{
A* Fred = new B;
delete Fred; // calls ~B(), not ~A()



IIRC,~A()仍将在B对象被破坏后调用。

IIRC, ~A() will still be called after the B object is destructed.


这篇关于关于默认析构函数的问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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