如何摆脱虚拟功能上烦人的编译警告 [英] How to get rid of the annoying compiling warning on virtual function

查看:210
本文介绍了如何摆脱虚拟功能上烦人的编译警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用英特尔C ++编译器9来编译项目。但是,有很多警告说像虚拟功能覆盖意图......。

。我在Google群组中搜索了旧邮件,有人已经在讨论这个问题。\\ b
问题。但我仍然感到困惑..


这是我从这些邮件中复制的简单测试代码

///////////// ////////////////////////////////////////////////// /////

class Base {

virtual void foo(); // *位置A *

};

类Derived1:Base {

virtual void foo(int); // *位置B *

};

类Derived2:Derived1 {

void foo(int); // *位置C *

};

////////////////////////// //////////////////////////////////////////


编译器将报告两个警告:


警告一:


警告#1125:functionBase :: foo( )"被Derived1 :: foo隐藏 -

虚拟功能覆盖意图?


警告二:


警告#1125:功能基地:: foo的()"被Derived2 :: foo隐藏 -

虚拟函数覆盖意图?


在我看来,*覆盖*仅发生在两个函数之间

有相同的签名和参数列表。似乎函数

原型在类Base和类Derived1之间是不同的。


如果你能给我一些解释,我将非常感激。感谢

你阅读我的帖子。

解决方案

" asm23" < as ******** @ gmail.comwrote in message

news:gc ********** @ aioe.org ...
< blockquote class =post_quotes>
您好,我正在使用英特尔C ++编译器9来编译项目。但是,有一个

警告说虚拟功能覆盖意图......。我在Google群组中搜索了旧邮件,有人已经在讨论这个问题。\\ b
问题。但我仍然感到困惑..


这是我从这些邮件中复制的简单测试代码

///////////// ////////////////////////////////////////////////// /////

class Base {

virtual void foo(); // *位置A *

};


类Derived1:Base {

virtual void foo(int); // *位置B *

};


类Derived2:Derived1 {

void foo(int); // *位置C *

};

////////////////////////// //////////////////////////////////////////


编译器将报告两个警告:


警告一:


警告#1125:functionBase :: foo( )"被Derived1 :: foo隐藏 -

虚拟功能覆盖意图?


警告二:


警告#1125:功能基地:: foo的()"被Derived2 :: foo隐藏 -

虚拟函数覆盖意图?


在我看来,*覆盖*仅发生在两个函数之间

有相同的签名和参数列表。似乎函数

原型在类Base和类Derived1之间是不同的。


如果你能给我一些解释,我将非常感激。谢谢

阅读我的帖子。



不幸的是你的意见是什么以及事实是什么都不会发生在这个问题上。如果您具有相同的功能名称,无论签名是什么,

您将通过派生函数隐藏基本功能。

的最佳工作是,将你的函数命名为派生的不同。除非你实际上想要多态性,否则修复签名并在基础中使用虚拟的

关键字。

On 2008-10-09 13:57:15 -0400,Jim Langston < ta ******* @ rocketmail.comsaid:


" asm23" < as ******** @ gmail.com在消息新闻中写道:gc ********** @ aioe.org ...


>我正在使用英特尔C ++编译器9来编译项目。但是,有很多警告说像虚拟功能覆盖
意图......。我搜索了Google小组中的旧消息,有人已经在谈论这个问题了。但我仍然感到困惑..

这是我从这些消息中复制的简单测试代码
///////////////////// /////////////////////////////////////////////// class Base {
virtual void foo(); // *位置A *
};

类Derived1:Base {
virtual void foo(int); // *位置B *
};

类Derived2:Derived1 {
void foo(int); // *位置C *
};
////////////////////////////////// //////////////////////////////////

编译器将报告两个警告:<警告一:

警告#1125:function" Base :: foo()"被Derived1 :: foo隐藏 -
虚拟功能覆盖意图?

警告二:

警告#1125:function" Base :: foo()"被Derived2 :: foo隐藏 -
虚拟功能覆盖意图?

在我看来,*覆盖*只发生在两个具有相同签名和参数列表的功能之间。似乎类Base和类Derived1之间的
函数原型是不同的。

如果你能给我一些解释,我将非常感激。感谢
你阅读我的帖子。



不幸的是你的意见是什么以及事实是什么都不会发生

来对此进行网格划分。



不,这是正确的:派生类中的函数会覆盖具有相同名称的基类中的虚拟

函数和签名。派生类中的函数

与基类中的函数同名但是

a不同的签名隐藏了基类中的一个。


如果你有相同的函数名称,无论签名如何,你将使用派生函数隐藏基函数。最好的工作

左右,将你的函数命名为派生的东西。

除非你真的想要多态,否则修复签名和

在基础中使用virtual关键字。



同意:隐藏功能通常会反映设计错误。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:作者:教程和参考资料的作者
www.petebecker.com/tr1book


10月9日上午10:39 * am, Pete Becker< p ... @ versatilecoding.comwrote:


On 2008-10-09 13:57:15 -0400," Jim Langston" < tazmas ... @ rocketmail.comsaid:


*如果你有相同的功能名称,请注意签名,你

将通过派生函数隐藏基函数。



同意:隐藏函数通常会反映出设计错误。



补充你们所说的:在派生的

类中定义的任何名称隐藏任何成员对象或函数

基数中的同名;除非它是虚拟功能并具有匹配功能

签名。


struct B

{

int object;

void func(){}

};


struct D:B

{

void object(){} //隐藏Base :: object

int func; //隐藏Base :: func

};


int main()

{

D d;

// d.object = 5;错误

// d.func();错误

}

阿里


Hi, I''m using Intel C++ compiler 9 to compiler a project. But, there are
a lot of warning saying like "virtual function override intended....". I
searched old messages in Google groups, someone already talk about this
issue. But I still confused..

This is the simple test code I copied from these messages
////////////////////////////////////////////////////////////////////
class Base {
virtual void foo(); //*position A*
};
class Derived1 : Base {
virtual void foo(int); //*position B*
};
class Derived2 : Derived1 {
void foo(int); //*position C*
};
////////////////////////////////////////////////////////////////////

The compiler will report two warnings:

Warning one:

warning #1125: function "Base::foo()" is hidden by "Derived1::foo" --
virtual function override intended?

Warning Two:

warning #1125: function "Base::foo()" is hidden by "Derived2::foo" --
virtual function override intended?

In my opinion, *overriding* is only occurred between two functions which
have the same signature and parameter list. It seems that the function
prototype are different between class Base and class Derived1.

I will be very appreciated if you can give me some explanation. Thank
you for reading my post.

解决方案

"asm23" <as********@gmail.comwrote in message
news:gc**********@aioe.org...

Hi, I''m using Intel C++ compiler 9 to compiler a project. But, there are a
lot of warning saying like "virtual function override intended....". I
searched old messages in Google groups, someone already talk about this
issue. But I still confused..

This is the simple test code I copied from these messages
////////////////////////////////////////////////////////////////////
class Base {
virtual void foo(); //*position A*
};
class Derived1 : Base {
virtual void foo(int); //*position B*
};
class Derived2 : Derived1 {
void foo(int); //*position C*
};
////////////////////////////////////////////////////////////////////

The compiler will report two warnings:

Warning one:

warning #1125: function "Base::foo()" is hidden by "Derived1::foo" --
virtual function override intended?

Warning Two:

warning #1125: function "Base::foo()" is hidden by "Derived2::foo" --
virtual function override intended?

In my opinion, *overriding* is only occurred between two functions which
have the same signature and parameter list. It seems that the function
prototype are different between class Base and class Derived1.

I will be very appreciated if you can give me some explanation. Thank you
for reading my post.

Unfortunately what you opionion is and what the facts are don''t happen to
mesh on this. If you have the same function name, reguardless of signature,
you will hide a base function by the derived function. The best work around
is, name your function something different in your derived. Unless you
actually intended polymorphism, then fix the signatures and use the virtual
keyword in the base.


On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rocketmail.comsaid:

"asm23" <as********@gmail.comwrote in message news:gc**********@aioe.org...

>Hi, I''m using Intel C++ compiler 9 to compiler a project. But, there
are a lot of warning saying like "virtual function override
intended....". I searched old messages in Google groups, someone
already talk about this issue. But I still confused..

This is the simple test code I copied from these messages
////////////////////////////////////////////////////////////////////
class Base {
virtual void foo(); //*position A*
};
class Derived1 : Base {
virtual void foo(int); //*position B*
};
class Derived2 : Derived1 {
void foo(int); //*position C*
};
////////////////////////////////////////////////////////////////////

The compiler will report two warnings:

Warning one:

warning #1125: function "Base::foo()" is hidden by "Derived1::foo" --
virtual function override intended?

Warning Two:

warning #1125: function "Base::foo()" is hidden by "Derived2::foo" --
virtual function override intended?

In my opinion, *overriding* is only occurred between two functions
which have the same signature and parameter list. It seems that the
function prototype are different between class Base and class Derived1.

I will be very appreciated if you can give me some explanation. Thank
you for reading my post.


Unfortunately what you opionion is and what the facts are don''t happen
to mesh on this.

No, it''s correct: a function in a derived class overrides a virtual
function in a base class with the same name and signature. A function
in a derived class with the same name as a function in a base class but
a different signature hides the one in the base class.

If you have the same function name, reguardless of signature, you
will hide a base function by the derived function. The best work
around is, name your function something different in your derived.
Unless you actually intended polymorphism, then fix the signatures and
use the virtual keyword in the base.

Agreed: hiding a function often reflects a design error.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


On Oct 9, 10:39*am, Pete Becker <p...@versatilecoding.comwrote:

On 2008-10-09 13:57:15 -0400, "Jim Langston" <tazmas...@rocketmail.comsaid:

* If you have the same function name, reguardless of signature, you
will hide a base function by the derived function.


Agreed: hiding a function often reflects a design error.

To complement what you both said: any ''name'' defined in the derived
class hides any member object or function having the same name in the
base; unless it''s a virtual function and has a matching function
signature.

struct B
{
int object;
void func() {}
};

struct D : B
{
void object() {} // hides Base::object
int func; // hides Base::func
};

int main()
{
D d;
// d.object = 5; ERROR
// d.func(); ERROR
}
Ali


这篇关于如何摆脱虚拟功能上烦人的编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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