473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 13090
"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.

Oct 9 '08 #2
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)

Oct 9 '08 #3
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
Oct 9 '08 #4
Pete Becker wrote:
On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rocketmail.comsaid:
>"asm23" <as********@gmail.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.
[...]
>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. [...]
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...@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
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*******@rocketmail.com>
said:
>>"asm23" <as********@gmail.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.
[...]
>>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. [...]

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
Rolf Magnus wrote:
Hendrik Schober wrote:
>Pete Becker wrote:
>>On 2008-10-09 13:57:15 -0400, "Jim Langston" <ta*******@rocketmail.com>
said:
"asm23" <as********@gmail.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.
>
[...]

To me, it also seems correct. Are you maybe mixing up overriding and hiding?
I was thinking of hiding:

class Base {
public:
virtual void f(int);
};

class Derived : public Base {
public:
virtual void f(double);
};

But that's not overriding, so I was wrong.

Schobi
Oct 14 '08 #11

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

Similar topics

2
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...
1
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 >...
11
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 =...
7
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...
10
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...
10
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
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...
7
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.