473,385 Members | 2,005 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,385 software developers and data experts.

Confused with the "virtual base class" concept

Hi,

I get an error "Warning: b::a_method hides the virtual function
a::a_method()."

on compiling the following code..

#include <iostream.h>
class a
{
public:
virtual int a_method ()
{
cout << "a::a_method" << endl;
return 0;
}
};

class b : public a
{
public:
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
};

class c : public a
{
public:
int a_method ()
{
cout << "c::a_method" << endl;
return 100;
}
};

int main()
{
b b_var;
b_var.a_method(10);
return 0;
}

The warning goes off if done the following:
Change class b : public a
to
class b : public virtual a

Have not been to understand the reason why the warning is removed as
"b::a_method still hides a::a_method()"

Would help if I could get some input.

Thanks and Regards,
M Shetty

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
14 1871
On 29 Aug 2004 18:46:06 -0400 in comp.lang.c++, ms*****@mail.com
(mshetty) wrote,
I get an error "Warning: b::a_method hides the virtual function
a::a_method()."


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[23.6] What's the meaning of, Warning: Derived::f(float) hides
Base::f(int)?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #2

"David Harmon" <so****@netcom.com.invalid> wrote in message
news:41***************@news.east.earthlink.net...
On 29 Aug 2004 18:46:06 -0400 in comp.lang.c++, ms*****@mail.com
(mshetty) wrote,
I get an error "Warning: b::a_method hides the virtual function
a::a_method()."


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[23.6] What's the meaning of, Warning: Derived::f(float) hides
Base::f(int)?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/


Probably you are missing his question. He does know that the function is
being hidden, his question is about warnings getting removed after making
class A a virtual base.
To OP - Which compiler ? I get a warning with Comeau even after making it a
virtual base - "warning: function "a::a_method()" is hidden by
"b::a_method" -- virtual function override intended? int a_method (int x)"

-Sharad

Jul 22 '05 #3
> I get an error "Warning: b::a_method hides the virtual function
a::a_method()." class b : public a
{
public:
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
};


I don't know why virtual inheritance removes this, but when you declare an
overloaded function in derived class (b::a_method(int)) it hides all
functions with the same name from base class (a::a_method()). For example,
you can't call b.a_method(); If you include using a::a_method; in b
definition, a::a_method won't become hidden. Btw, you probably want to
declare a_method(int) as virtual, too, don't you?

--
Ivan

e-mail me at: korotkov2 at ztel dot ru
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4
mshetty <ms*****@mail.com> wrote:
Hi,

I get an error "Warning: b::a_method hides the virtual function
a::a_method()."

on compiling the following code..

#include <iostream.h>
class a
{
public:
virtual int a_method ()
{
cout << "a::a_method" << endl;
return 0;
}
};

class b : public a
{
public:
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
}

note well a::a_method() is a function with no args retuning int, and
b::a_method(int) is a function with an int arg returning int. Since b
has no method a_method() but one of a_method(int); it will hide [not
have available] the virtual function a_method(), if want the
vritual-ness of a_method() you must have such a function in b, since you
already have a a_method(int) which hides the defaulting a::method() of
being used, If you really want b::a_method_a(int) as well, then you need
a forwarding member function
void a_method() {a::a_method();} as well.

Virtual bases are used to provide just one base class of a type rather
then multiples in cases like this:

class Base{};
class Inbetween_1:public virtual Base{};
class Inbetween_2:public virtual Base{};
class TheWinner:public Inbetween_1,public Inbetween_2{};

this makes one instande of Base in an instance of TheWinner;

if the Inbetween's above were to each inherit Base non-virtually then
TheWinner would have two instances of Base namely
Inbetween_1::Base and Inbetween_2::Base. The class diagram of the
virtual inheritance case looks like a rhombus, and the non virtual
inheritance diagram looks like a Y.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5
mshetty <ms*****@mail.com> wrote:

[]
The warning goes off if done the following:
Change class b : public a
to
class b : public virtual a

Have not been to understand the reason why the warning is removed as
"b::a_method still hides a::a_method()"


It looks like poor diagnostic.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6
I have added a line in class b.
read it.
hth
david michell

#include <iostream.h>
class a
{
public:
virtual int a_method ()
{
cout << "a::a_method" << endl;
return 0;
}
};

class b : public a
{
public:
virtual int a_method (){} /*ADD THIS LINE becaue
class c has to know that this method is virtual. It will not be taken
as default by inheritance*/
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
};

class c : public a
{
public:
int a_method ()
{
cout << "c::a_method" << endl;
return 100;
}
};

int main()
{
b b_var;
b_var.a_method(10);
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #7
ms*****@mail.com (mshetty) wrote in message news:<bf**************************@posting.google. com>...
Hi,

I get an error "Warning: b::a_method hides the virtual function
a::a_method()."

on compiling the following code..

#include <iostream.h>
class a
{
public:
virtual int a_method ()
{
cout << "a::a_method" << endl;
return 0;
}
};

class b : public a
{
public:
int a_method (int x)
{
cout << "b::a_method" << endl;
return x;
}
};

class c : public a
{
public:
int a_method ()
{
cout << "c::a_method" << endl;
return 100;
}
};

int main()
{
b b_var;
b_var.a_method(10);
return 0;
}

The warning goes off if done the following:
Change class b : public a
to
class b : public virtual a

Have not been to understand the reason why the warning is removed as
"b::a_method still hides a::a_method()"

Would help if I could get some input.


Hi, mshetty. It is a silly warning, you can simply ignore it. For
example the Visual Studio .NET with 4 level (highest) of warning
messages did not generate it. But it is pretty-clear allusion to bad
design of your program.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #8
> public:
virtual int a_method (){} /*ADD THIS LINE becaue
class c has to know that this method is virtual. It will not be taken
as default by inheritance*/


class C inherits A directly, or is it a typo? Anyway, overriding a_method
as empty is not excellent, it's better to import it from A by using A::f;
in class B. Btw, which compiler did you use? VC++ understands that
a_method is virtual:

#include <iostream>

using namespace std;

class A { public : virtual void f() { cout << "In A::f\n"; } };
class B : public A { public: void f(int) {} };
class C : public B { public: void f() { cout << "In C::f\n"; }};

int main()
{
C c;

static_cast<A *>(&c)->f();

return 0;
}

yields

In C::f

--
Ivan

e-mail me at: korotkov2 at ztel dot ru

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #9
ms*****@mail.com (mshetty) wrote in message news:<bf**************************@posting.google. com>...
Hi,

I get an error "Warning: b::a_method hides the virtual function
a::a_method()."

on compiling the following code..
[snip]
The warning goes off if done the following:
Change class b : public a
to
class b : public virtual a

Have not been to understand the reason why the warning is removed as
"b::a_method still hides a::a_method()"


As others have pointed out, b::a_method() does hide a::a_method(), and
it seems it should hide it whether "a" is a virtual base of "b" or
not.

Intrigued by this, I tried your code under both GCC 3.4.1 and Sun CC
5.3 (WorkShop 6 Update 2) on Solaris 8, and was astonished by the
results:

`g++ -Wall -ansi -pedantic` compiled both the virtual and nonvirtual
versions without so much as a single warning (after changing
iostream.h to iostream and adding "using std::cout" and "using
std::endl" at the top). I tried this multiple times out of disbelief,
but the result was indeed the same each time; no mistyping on my part.

`CC +w2` produced the following results:

--BEGIN--
# CC +w2 virtual_base.cxx

"virtual_base.cxx", line 25: Warning: b::a_method hides the virtual
function a::a_method().
1 Warning(s) detected.

# vim virtual_base.cxx

[change base to virtual]

# CC +w2 virtual_base.cxx

"virtual_base.cxx", line 25: Warning: b::a_method Hides the virtual
function a::a_method() in a virtual base.
1 Warning(s) detected.
--END--

So now I'm even more confused. The Sun compiler seems to be right on
with this one, and GCC is oblivious, which is exactly backwards from
what I might've expected. What compiler and platform are you using,
M? Can anyone else provide some insight into what's going on?

Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #10
mb****@acm.org (Mike Bland) wrote in message news:<b8**************************@posting.google. com>...
So now I'm even more confused. The Sun compiler seems to be right on
with this one, and GCC is oblivious, which is exactly backwards from
what I might've expected.


Whoops, after looking at the GCC info page, I realized that what I
need to use is -Woverloaded-virtual. Wonder why it's not
automatically a part of -Wall.

Still, I'd be interested in knowing which compiler the OP was using.

Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #11
mb****@acm.org (Mike Bland) wrote in message news:<b8**************************@posting.google. com>...
mb****@acm.org (Mike Bland) wrote in message news:<b8**************************@posting.google. com>...
> So now I'm even more confused. The Sun compiler seems to be right on
> with this one, and GCC is oblivious, which is exactly backwards from
> what I might've expected.


Whoops, after looking at the GCC info page, I realized that what I
need to use is -Woverloaded-virtual. Wonder why it's not
automatically a part of -Wall.

Still, I'd be interested in knowing which compiler the OP was using.


Hi,

Details of the compiler and system I am using
Compiler : CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15
OS : SunOS pcspsun3 5.7 Generic_106541-34 sun4u sparc SUNW,Ultra-5_10

Regards,
M Shetty

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #12
mb****@acm.org (Mike Bland) wrote:
mb****@acm.org (Mike Bland) wrote:
> So now I'm even more confused. The Sun compiler seems to be right on
> with this one, and GCC is oblivious, which is exactly backwards from
> what I might've expected.


Whoops, after looking at the GCC info page, I realized that what I
need to use is -Woverloaded-virtual. Wonder why it's not
automatically a part of -Wall.


No diagnostic is required by the Standard for the case of a function
hiding a base function (it's merely a quality-of-implementation issue).

Also -Wall is unfortunately-named, it should be -Wa-few-common-problems.
You should probably read the g++ documentation and add in lots more
-W settings to your Makefile.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #13
ms*****@mail.com (mshetty) wrote in message news:<bf**************************@posting.google. com>...
Details of the compiler and system I am using
Compiler : CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15
OS : SunOS pcspsun3 5.7 Generic_106541-34 sun4u sparc SUNW,Ultra-5_10


Interesting. We're using the same Sun compiler, but I'm on patch
111685-20 dated 2004/03/19. You're using Solaris 7 and I'm on Solaris
8. Could be an issue with the compiler patchlevel, too. Try hitting
http://access1.sun.com/sundev/fdp6u2-patches.html to upgrade your
compiler patchlevel and see what happens.

Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #14
ol*****@inspire.net.nz (Old Wolf) wrote in message news:<84**************************@posting.google. com>...
No diagnostic is required by the Standard for the case of a function
hiding a base function (it's merely a quality-of-implementation issue).
That's kinda surprising. Do you know if there are any plans to add
such a requirement?
Also -Wall is unfortunately-named, it should be -Wa-few-common-problems.
You should probably read the g++ documentation and add in lots more
-W settings to your Makefile.


LOL That's how I realized, eventually, that -Wall didn't contain
-Woverloaded-virtual. I mean, I've known for a long time that -Wall
didn't really turn on *all* warnings, but I was quite suprised when I
looked at the info page after my initial posting and realized it
didn't include this one by default.

Incidentally, I use a "pedantic" script that precompiles our sources
with g++ with various warnings explicitly turned on before compiling
them with Sun CC. It's worked out quite well for shaking out all
sorts of nasty errors and warnings.

Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #15

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

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
8
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.