egcs-1.1 and virtual method questions

Mike Stump mrs@wrs.com
Wed Nov 18 15:16:00 GMT 1998


> Date: Wed, 18 Nov 98 21:00:09 +0100
> From: Gilles Depeyrot <Gilles.Depeyrot@wanadoo.fr>
> To: <egcs-bugs@cygnus.com>

> I have a question concerning egcs 1.1 and virtual methods. There
> may be a bug in egcs but I need more background.

Nope, no bug.

> In a 20K+ pile of C++ code which I am supposed to maintain (I didn't
> write the stuff), I have a problem with virtual methods not acting
> as expected.

You don't give an testcase, we can't help you easily without it.
comp.lang.c++ is a better place to learn C++ than this list.

> Given the base class:

> class foo {
> public:
>      foo();
>      ~foo();

>      virtual bool do_something();
> };

> and the class:

> class foobar : public foo {
> public:
>      foobar();
>      ~ foobar();

>      bool do_something();
> };

> You can notice that the virtual method of class foo is overridden in
> class foobar but is *not* declared as virtual.

Not a problem.

> If I manipulate objects of class foobar with a foo pointer, which method
> should be called for do_something ?

One or the other depending on how you call it.  foo::do_something()
calls one, and do_something calls the other.  This is from C++ 101.

Your foo dtor should be virtual if you _ever_ do a delete foo_ptr;!

> Shouldn't the compiler emit a warning because do_something is overridden
> but not declared virtual ?

No.

> In my code, with an object of class foobar manipulated with a foo
> pointer, the do_something method which is effectively called is
> foo's do_something instead of foobar's do_something !

And without and detail, we can't help.  Here are the possibilities:

    1)  You called it non-virtually  (foo::do_something()),
    2)  The object's type is in fact foo, you just are confused and thing
	it is foobar,
    3)  A compiler bug slicing when it shouldn't (unlikely),
    4)  Vtable filling problem (unlikely).



More information about the Gcc-bugs mailing list