不幸的是你的意见是什么以及事实是什么都不会发生 来对此进行网格划分。 不,这是正确的:派生类中的函数会覆盖具有相同名称的基类中的虚拟 函数和签名。派生类中的函数 与基类中的函数同名但是 a不同的签名隐藏了基类中的一个。 同意:隐藏功能通常会反映设计错误。 - Pete Roundhouse Consulting,Ltd。( www.versatilecoding.com ) 标准C ++库扩展:作者:教程和参考资料的作者( www.petebecker.com/tr1book ) 同意:隐藏函数通常会反映出设计错误。 补充你们所说的:在派生的 类中定义的任何名称隐藏任何成员对象或函数 基数中的同名;除非它是虚拟功能并具有匹配功能 签名。 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 area lot of warning saying like "virtual function override intended....". Isearched old messages in Google groups, someone already talk about thisissue. 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 whichhave the same signature and parameter list. It seems that the functionprototype are different between class Base and class Derived1.I will be very appreciated if you can give me some explanation. Thankyou for reading my post. 解决方案Unfortunately what you opionion is and what the facts are don''t happen tomesh 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 aroundis, name your function something different in your derived. Unless youactually intended polymorphism, then fix the signatures and use the virtualkeyword in the base.Unfortunately what you opionion is and what the facts are don''t happento mesh on this.No, it''s correct: a function in a derived class overrides a virtualfunction in a base class with the same name and signature. A functionin a derived class with the same name as a function in a base class buta different signature hides the one in the base class.Agreed: hiding a function often reflects a design error.--PeteRoundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "TheStandard C++ Library Extensions: a Tutorial and Reference(www.petebecker.com/tr1book)Agreed: hiding a function often reflects a design error.To complement what you both said: any ''name'' defined in the derivedclass hides any member object or function having the same name in thebase; unless it''s a virtual function and has a matching functionsignature.struct B{int object;void func() {}};struct D : B{void object() {} // hides Base::objectint func; // hides Base::func};int main(){D d;// d.object = 5; ERROR// d.func(); ERROR}Ali 这篇关于如何摆脱虚拟功能上烦人的编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 17:33