egcs-1.1 and virtual method questions
Martin von Loewis
Wed Nov 18 17:05:00 GMT 1998
> You can notice that the virtual method of class foo is overridden in
> class foobar but is *not* declared as virtual.
Doesn't matter, you only need to declare it virtual in the base.
> If I manipulate objects of class foobar with a foo pointer, which method
> should be called for do_something ?
The function of the derived class.
> Shouldn't the compiler emit a warning because do_something is overridden
> but not declared virtual ?
No, this is valid C++, and frequently used.
> 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 !
That would depend on the code. For example, if you do the virtual call
in a constructor, things are different. Also, if you create a foo object
before calling the method, you get foo::do_something.
If it is neither of these, report it as a bug.
More information about the Gcc-bugs