派生类的虚拟关键字 [英] virtual keyword for the derived class

查看:73
本文介绍了派生类的虚拟关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我不确定派生类的虚拟关键字是否需要

给定

表示基类已经宣布它是虚拟的。


A级

{

公开:

虚拟〜 A();

}


B级:公共A

{

public:

虚拟~B();

}


C级:公共B

{

公开:

虚拟~C();

}


我的问题是我是否需要B类和C类的虚拟关键字?如果

我不需要它,是否建议总是添加虚拟关键字?

它是否有任何负面影响?


谢谢。

邵。

Hi,

I am not sure if the virtual keyword for the derived classes are
required given
that the base class already declares it virtual.

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

class B : public A
{
public:
virtual ~B();
}

class C: public B
{
public:
virtual ~C();
}

My question is whether I need the virtual keyword for class B and C? If
I don''t need it, is it recommanded to always add the virtual keyword?
Does it have any negative effect at all?

Thanks.
Shao.

推荐答案

" Shao Zhang" < SH ** @ cia.com.au>在消息中写道

news:cd ******** @ odbk17.prod.google.com ...
"Shao Zhang" <sh**@cia.com.au> wrote in message
news:cd********@odbk17.prod.google.com...

我不确定是否需要派生类的虚拟关键字,因为基类已经将其声明为虚拟。

A类
公共:
虚拟~A();
}

B类:公众A
公开:
虚拟~B ();
}

C班:公共B
{
公开:
虚拟~C();
}

我的问题是我是否需要B类和C类的虚拟关键字?如果我不需要它,是否建议总是添加虚拟关键字?
它是否有任何负面影响?

谢谢。
邵。
Hi,

I am not sure if the virtual keyword for the derived classes are
required given
that the base class already declares it virtual.

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

class B : public A
{
public:
virtual ~B();
}

class C: public B
{
public:
virtual ~C();
}

My question is whether I need the virtual keyword for class B and C? If
I don''t need it, is it recommanded to always add the virtual keyword?
Does it have any negative effect at all?

Thanks.
Shao.




这不是必需的,但我认为明确声明所有虚拟

函数是个好主意。这让我更容易理解恕我直言。


-

Cy
http://home.rochester.rr.com/cyhome/


Cy Edmunds < CE ****** @ spamless.rochester.rr.com>写在消息

新闻:lY%Kc.51174
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:lY%Kc.51174
这不是必需的,但我认为宣布所有虚拟是个好主意
明确的功能。这让我更容易理解恕我直言。
It''s not required, but I think it''s a good idea to declare all virtual
functions explicitly. It makes it a little easier to understand IMHO.




我经常使用评论,但两种方式都不比另一方好。


class X:public Y {

protected://从X继承的虚函数
bool equal()const;

};



Often I use comments, though neither way is better than the other.

class X: public Y {
protected: // virtual functions inherited from X
bool equal() const;
};


Cy Edmunds写道:
Cy Edmunds wrote:
我的问题是我是否需要B类的虚拟关键字和C?
如果我不需要它,是否建议始终添加虚拟
关键字?是否有任何负面影响?

谢谢。
邵。
My question is whether I need the virtual keyword for class B and C?
If I don''t need it, is it recommanded to always add the virtual
keyword? Does it have any negative effect at all?

Thanks.
Shao.



这不是必需的,但我认为它''最好明确声明所有虚拟
函数。这让我更容易理解恕我直言。



It''s not required, but I think it''s a good idea to declare all virtual
functions explicitly. It makes it a little easier to understand IMHO.




重点是,你可以看到该功能是虚拟的,而不需要
首先查看基类(或基类的基础或

基数'的基数或最初声明为虚拟的地方)。

恕我直言,如果C ++在这个

的地方需要虚拟关键字会更好。



The main point is that you can see that the function is virtual without
having to look into the base class first (or the base''s base or the
base''s base''s base or wherever it was initially declared virtual).
IMHO it would be better if C++ required the virtual keyword in this
place.


这篇关于派生类的虚拟关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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