473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get rid of the annoying compiling warning on virtual function

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.
Oct 9 '08 #1
10 13209
"asm23" <as********@gma il.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.

Oct 9 '08 #2
On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rock etmail.comsaid:
"asm23" <as********@gma il.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)

Oct 9 '08 #3
On Oct 9, 10:39*am, Pete Becker <p...@versatile coding.comwrote :
On 2008-10-09 13:57:15 -0400, "Jim Langston" <tazmas...@rock etmail.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
Oct 9 '08 #4
Pete Becker wrote:
On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rock etmail.comsaid:
>"asm23" <as********@gma il.comwrote in message news:gc******** **@aioe.org...
[...]
>>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.
[...]
>Unfortunatel y 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. [...]
Yes, bat asm23 said that /only/ such a function would
override, and that, unless I'm missing something, is
not correct.
[...]
Schobi
Oct 9 '08 #5
ac******@gmail. com wrote:
On Oct 9, 10:39 am, Pete Becker <p...@versatile coding.comwrote :
>On 2008-10-09 13:57:15 -0400, "Jim Langston" <tazmas...@rock etmail.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
Hi, Ali. Thanks for supply an example for us. As Pete said, "If you have
the same function name, regardless of signature,you will hide a base
function by the derived function." This is a important rule I would
remember. Unless a derived class have a "virtual function has a matching
function name and signature"

Thanks for helping.
Oct 9 '08 #6
asm23 wrote:
Hi, Ali. Thanks for supply an example for us. As Pete said, "If you have
the same function name, regardless of signature,you will hide a base
function by the derived function." This is a important rule I would
remember. Unless a derived class have a "virtual function has a matching
function name and signature"
Just remember the member function has to be declared virtual in the base
class.

--
Ian Collins
Oct 9 '08 #7
Ian Collins wrote:
asm23 wrote:
>Hi, Ali. Thanks for supply an example for us. As Pete said, "If you have
the same function name, regardless of signature,you will hide a base
function by the derived function." This is a important rule I would
remember. Unless a derived class have a "virtual function has a matching
function name and signature"
Just remember the member function has to be declared virtual in the base
class.
Do you mean that this is the *precondition* to apply the rule?
Oct 9 '08 #8
asm23 wrote:
Ian Collins wrote:
>asm23 wrote:
>>Hi, Ali. Thanks for supply an example for us. As Pete said, "If you have
the same function name, regardless of signature,you will hide a base
function by the derived function." This is a important rule I would
remember. Unless a derived class have a "virtual function has a matching
function name and signature"
Just remember the member function has to be declared virtual in the base
class.
Do you mean that this is the *precondition* to apply the rule?
What I mean is you can't change a non-virtual base class member function
into a virtual member function in a derived class.

--
Ian Collins
Oct 9 '08 #9
Hendrik Schober wrote:
Pete Becker wrote:
>On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rock etmail.com>
said:
>>"asm23" <as********@gma il.comwrote in message
news:gc****** ****@aioe.org.. .
[...]
>>>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.
[...]
>>Unfortunate ly 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. [...]

Yes, bat asm23 said that /only/ such a function would
override, and that, unless I'm missing something, is
not correct.
To me, it also seems correct. Are you maybe mixing up overriding and hiding?

Oct 11 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1708
by: IK | last post by:
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a mechanism by which interface and implementation is seperated. A basic com implementation in c++ can be as follows. // Dev-CPP // interface
1
2337
by: A. Saksena | last post by:
Hi all, I am trying to compile following code :- ======================================= #include <iostream.h> #include <string> namespace tlm { template <typename REQUEST, typename RESPONSE > class tlm_transport_if {
11
1782
by: Achintya | last post by:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& #include<iostream> using namespace std; class A { int i; virtual void f(){ cout<<endl<<"i= "<<i; } public: A(int j) { i = j; }
7
1772
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has a virtual destructor, and therefore, a pointer to a virtual function table.
10
2970
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's constructor change its behaviour accounting to the derived class. I tried to make A::fun() not pure virtual but virtual. It doesn't generate any error. But A::fun() is called in A's construction, while I
10
7295
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
3
1727
by: trialproduct2004 | last post by:
Hi all, Can someone tell me how virtual functions works. Like how memory is getting allocated to virtual function. And how to call base class function through derived class pointer. And why virtual function can be access only through pointer. Thanks in advance.
7
341
by: dragoncoder | last post by:
What are your feeling about the code ? #include <iostream> #include <ostream> using namespace std; class A { public: A() { f(); }
6
2829
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that a user-defined class has overriden the virtual function in the base class? I have code like this which compiles with gcc 2.95.4: Attrib Implicits(PadRcv *obj){ Attrib accum = 0;
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.