为什么我们需要非虚拟析构函数? [英] Why do we need non-virtual destructor?

查看:90
本文介绍了为什么我们需要非虚拟析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们需要非虚拟析构函数?

因为vtable?


Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library /view/10978.html
http://sourceforge.net/ users / alexvn

Why do we need non-virtual destructor?
Because of vtable?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

推荐答案



Alex Vinokur写道:

Alex Vinokur wrote:
为什么我们需要非虚拟析构函数?
因为vtable?
Why do we need non-virtual destructor?
Because of vtable?




究竟是什么问题?


如果类是多态的,析构函数应该是虚拟的,这样你就可以通过指向

基类的指针正确地删除派生类的对象。


如果类不是多态的(因此不打算用作

基类)那么析构函数s应该是非虚拟的。



What exactly are you asking?

The destructor should be virtual if the class is polymorphic, so that
you can properly delete the derived class''s object through pointer to
the base class.

If the class is not polymorphic (and hence not intended to be used as
base class) then destructor should be non-virtual.




" Neelesh Bodas" <是ne *********** @ gmail.com>在留言新闻中写道:11 ********************** @ g47g2000cwa.googlegr oups.com ...

"Neelesh Bodas" <ne***********@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...

Alex Vinokur写道:

Alex Vinokur wrote:
为什么我们需要非虚拟析构函数?
因为vtable?
你究竟要问什么?
Why do we need non-virtual destructor?
Because of vtable?
What exactly are you asking?



如果有情况我们_must_使用_non-virtual_析构函数?
如果类是多态的,析构函数应该是虚拟的,这样你就可以正确删除派生类通过指向基类的指针。

如果类不是多态的(因此不打算用作基类)那么析构函数应该是非的-virtual。



If are there situations that we _must_ use _non-virtual_ destructor?

The destructor should be virtual if the class is polymorphic, so that
you can properly delete the derived class''s object through pointer to
the base class.

If the class is not polymorphic (and hence not intended to be used as
base class) then destructor should be non-virtual.




-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Alex Vinokur写道:
Alex Vinokur wrote:
" Neelesh Bodas" <是ne *********** @ gmail.com>写道:
"Neelesh Bodas" <ne***********@gmail.com> wrote:

Alex Vinokur写道:

Alex Vinokur wrote:
为什么我们需要非虚拟析构函数?
因为vtable?
Why do we need non-virtual destructor?
Because of vtable?


你究竟在问什么?



What exactly are you asking?



如果有情况我们_must_使用_non-virtual_析构函数?



If are there situations that we _must_ use _non-virtual_ destructor?




最多...


为什么你会期望以Point类为基础?


class Point

{

public:

Point(double x,double y):_ x(x),_ y(y){}

~点()

// ...

私人:

double _x;

double _y;

}


有很多非polimorphic类,OOP中使用的类

结构(即设计模式)期望使用

继承。因此,在这种情况下,你不使用虚拟构造函数

(一般来说是虚拟方法)。


-

Mateusz Loskot
http://mateusz.loskot.net



Most...

Why would you expect to use Point class as a base?

class Point
{
public:
Point(double x, double y) : _x(x), _y(y) {}
~Point()
//...
private:
double _x;
double _y;
}

There are plenty of non-polimorphic classes, classes used in OOP
constructions (i.e. design patterns) where you don''t expect to use
inheritance. So, in such cases you don''t use virtual constructors
(virtual methods, in general).

--
Mateusz Loskot
http://mateusz.loskot.net


这篇关于为什么我们需要非虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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