Redudant虚拟关键字? [英] Redudant virtual keyword?
问题描述
嗨伙计们!
每次我使用虚拟和纯虚拟方法时,我都会问自己是否b / B和C类之间存在差异?见下文)。在派生类中重复虚拟关键字是多余的吗?或者,我需要考虑的是否有任何差异?
当我从外部来源阅读
源代码时,我已经看到了两种方式。到目前为止,我没有找到任何答案
我的书或网上。
A级{
public :
虚拟空虚foo()= 0;
虚拟空格条(){}
};
B级{
公开:
void foo(){}
void bar(){}
};
class C {
public:
virtual void foo(){}
虚拟空栏(){}
};
CU,
Daniel Kay
" Daniel Kay" <哒******** @ arcor.de>在消息中写道
news:43 *********************** @ newsread2.arcor-online.net ...嗨伙计们!
每当我使用虚拟和纯虚拟方法时,我都会问自己,B类和C类之间是否存在差异(见下文)。在派生类中重复虚拟关键字是多余的吗?或者有什么区别我需要考虑?当我从外部源读取源代码时,我已经看到了两种方式。到目前为止,我没有在我的书籍或网上找到任何答案。
A类{
公开:
virtual void foo()= 0 ;
虚拟空栏(){}
};
B班{
公开:
void foo(){}
void bar(){}
};
从你的问题来看,我认为你的意思是:
B级:公开A {
class C {
public:
virtual void foo(){}
virtual void bar(){}
};
同样在这里:
class C:public B {//或:public A
正确吗?
如果是这样,那么虚拟关键字确实是多余的,因为派生类定义中不需要
。一旦在基础
类中声明为虚拟,它在派生类中保持虚拟,无论您是否包含
虚拟关键字。
作为标准练习,我总是在派生的
类中重复关键字,这样我就知道它是一个虚拟方法,而且不必看起来
回到我的基类,看看它是否在某个地方被宣布为虚拟。
(此外,我通常将函数声明复制并粘贴到我的派生中
$ b $无论如何b类,所以虚拟关键字与函数声明的其余部分一起被复制
。)
-Howard
Daniel Kay写道:每次我使用虚拟和纯虚拟方法时,我都会问自己,B类和C类之间是否有区别(见下文) )。在派生类中重复虚拟关键字是多余的吗?
从语言的角度来看,是的。从团队发展过程
的角度来看,没有。这是风格的东西。
[...]
V
< blockquote>
" Daniel Kay" <哒******** @ arcor.de>在消息中写道
news:43 *********************** @ newsread2.arcor-online.net ...
嗨伙计们!
每当我使用虚拟和纯虚拟方法时,我都会问自己,B类和C类之间是否存在差异(见下文)。在派生类中重复虚拟关键字是多余的吗?
是的,但这通常是为了清晰起见。
或者我需要考虑哪些差异?
无操作差异。
当我从外部源读取源代码时,我已经看到了两种方式。所以很远,我在书本或网上找不到任何答案。
哪些书?也许你需要更好的。
-Mike
Hi Folks!
Everytime I work with virtual and pure virtual methods I ask myself if
there is a difference between class B and class C (see below). Is it
redundant to repeat the virtual keyword in a derived class? Or is there
any difference I need to consider? I have seen both ways when I read
source code from external sources. So far I didn''t find any answers in
my books or on the net.
class A {
public:
virtual void foo() = 0;
virtual void bar() {}
};
class B {
public:
void foo() {}
void bar() {}
};
class C {
public:
virtual void foo() {}
virtual void bar() {}
};
CU,
Daniel Kay
"Daniel Kay" <da********@arcor.de> wrote in message
news:43***********************@newsread2.arcor-online.net...Hi Folks!
Everytime I work with virtual and pure virtual methods I ask myself if
there is a difference between class B and class C (see below). Is it
redundant to repeat the virtual keyword in a derived class? Or is there
any difference I need to consider? I have seen both ways when I read
source code from external sources. So far I didn''t find any answers in my
books or on the net.
class A {
public:
virtual void foo() = 0;
virtual void bar() {}
};
class B {
public:
void foo() {}
void bar() {}
};
From your question, I assume you meant:
class B : public A {
class C {
public:
virtual void foo() {}
virtual void bar() {}
};
And likewise here:
class C : public B { // or : public A
Correct?
If so, then the virtual keyword is indeed redundant, in that it is not
needed in the derived class definitions. Once declared virtual in a base
class, it remains virtual in derived classes, whether or not you include the
virtual keyword.
As a standard practice, though, I always repeat the keyword in derived
classes, just so that I KNOW it''s a virtual method, and don''t have to look
back through my base classes to see if it''s declared virtual somewhere.
(Besides, I usually copy & paste the function declarations into my derived
class anyway, so the virtual keyword gets copied right along with the rest
of the function declaration.)
-Howard
Daniel Kay wrote:Everytime I work with virtual and pure virtual methods I ask myself if
there is a difference between class B and class C (see below). Is it
redundant to repeat the virtual keyword in a derived class?
From the language point of view, yes. From a team development process
point of view, no. It''s a style thing.
[...]
V
"Daniel Kay" <da********@arcor.de> wrote in message
news:43***********************@newsread2.arcor-online.net...Hi Folks!
Everytime I work with virtual and pure virtual methods I ask myself if
there is a difference between class B and class C (see below). Is it
redundant to repeat the virtual keyword in a derived class?
Yes, but it''s often done in the interest of clarity.
Or is there any difference I need to consider?
No operational difference.
I have seen both ways when I read source code from external sources. So
far I didn''t find any answers in my books or on the net.
Which books? Perhaps you need better ones.
-Mike
这篇关于Redudant虚拟关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!