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

A

asm23

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.
 
J

Jim Langston

asm23 said:
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.
 
A

acehreli

On 2008-10-09 13:57:15 -0400, "Jim Langston" <[email protected]> said:

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::eek:bject
int func; // hides Base::func
};

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


Ali
 
H

Hendrik Schober

Pete said:
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
 
A

asm23

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::eek:bject
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.
 
I

Ian Collins

asm23 said:
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.
 
I

Ian Collins

asm23 said:
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.
 
R

Rolf Magnus

Hendrik said:
Pete said:
news:[email protected]... [...]
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?
 
H

Hendrik Schober

Rolf said:
Hendrik said:
Pete said:
On 2008-10-09 13:57:15 -0400, "Jim Langston" <[email protected]>
said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top