 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mshetty Guest
|
Posted: Sun Aug 29, 2004 10:46 pm Post subject: 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! ]
|
|
| Back to top |
|
 |
Ivan Korotkov Guest
|
Posted: Mon Aug 30, 2004 10:50 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Mon Aug 30, 2004 10:53 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
mshetty <mshetty (AT) mail (DOT) com> wrote:
| Quote: | Hi,
I get an error "Warning: b::a_method hides the virtual function
a::a_method()."
on compiling the following code..
#include
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! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Mon Aug 30, 2004 11:46 pm Post subject: Re: Confused with the "virtual base class" concept |
|
|
mshetty <mshetty (AT) mail (DOT) com> wrote:
[]
| Quote: | 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! ]
|
|
| Back to top |
|
 |
David Michell Guest
|
Posted: Mon Aug 30, 2004 11:51 pm Post subject: Re: Confused with the "virtual base class" concept |
|
|
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! ]
|
|
| Back to top |
|
 |
Vladislav Lazarenko Guest
|
Posted: Tue Aug 31, 2004 10:04 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mshetty (AT) mail (DOT) com[/email] (mshetty) wrote in message news:<bfbb8fd4.0408290554.551900f8 (AT) posting (DOT) google.com>...
| Quote: | Hi,
I get an error "Warning: b::a_method hides the virtual function
a::a_method()."
on compiling the following code..
#include
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! ]
|
|
| Back to top |
|
 |
Ivan Korotkov Guest
|
Posted: Tue Aug 31, 2004 7:57 pm Post subject: Re: Confused with the "virtual base class" concept |
|
|
| Quote: | 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::fn"; } };
class B : public A { public: void f(int) {} };
class C : public B { public: void f() { cout << "In C::fn"; }};
int main()
{
C c;
static_castf();
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! ]
|
|
| Back to top |
|
 |
Mike Bland Guest
|
Posted: Wed Sep 01, 2004 2:55 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mshetty (AT) mail (DOT) com[/email] (mshetty) wrote in message news:<bfbb8fd4.0408290554.551900f8 (AT) posting (DOT) google.com>...
| Quote: | Hi,
I get an error "Warning: b::a_method hides the virtual function
a::a_method()."
on compiling the following code..
|
[snip]
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Mike Bland Guest
|
Posted: Fri Sep 03, 2004 11:03 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mbland (AT) acm (DOT) org[/email] (Mike Bland) wrote in message news:<b808d7c1.0408311035.2edde96e (AT) posting (DOT) google.com>...
| Quote: | 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! ]
|
|
| Back to top |
|
 |
mshetty Guest
|
Posted: Wed Sep 08, 2004 8:17 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mbland (AT) acm (DOT) org[/email] (Mike Bland) wrote in message news:<b808d7c1.0409020721.46a5216c (AT) posting (DOT) google.com>...
| Quote: | mbland (AT) acm (DOT) org (Mike Bland) wrote in message news:<b808d7c1.0408311035.2edde96e (AT) posting (DOT) 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! ]
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Thu Sep 09, 2004 9:58 am Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mbland (AT) acm (DOT) org[/email] (Mike Bland) wrote:
| Quote: | mbland (AT) acm (DOT) 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! ]
|
|
| Back to top |
|
 |
Mike Bland Guest
|
Posted: Fri Sep 10, 2004 1:34 pm Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]mshetty (AT) mail (DOT) com[/email] (mshetty) wrote in message news:<bfbb8fd4.0409071938.1df9c1c4 (AT) posting (DOT) google.com>...
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Mike Bland Guest
|
Posted: Fri Sep 10, 2004 1:40 pm Post subject: Re: Confused with the "virtual base class" concept |
|
|
[email]oldwolf (AT) inspire (DOT) net.nz[/email] (Old Wolf) wrote in message news:<843a4f78.0409081529.7ce9d3f2 (AT) posting (DOT) google.com>...
| Quote: | 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?
| Quote: | 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! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|