Topic: force the virtual keyword on virtual members


Author: konfjo@eua.ericsson.se (Fredrik Jonsson)
Date: 12 Jul 1994 07:06:17 GMT
Raw View
Why shouldn't virtual members in a base class be forced by the compiler to be
declared as virtuals when they are overloaded in a derived class? I have been
trying to find some recommendations about this but I have failed. In OOPascal
the virtual keyword has to be declared each time a virtual function is over-
loaded but in C++ you just have to declare it in the baseclass and after that
it doesn't make any difference if the virtual keyword exists or not in front
of the virtual member function!?

I think it should be there to make the class definition easier to understand.
Someone also suggested, (in the comp.lang.c++ area), that a "overload" keyword
could be introduced to indicate that one really want to overload a virtual. I
think it's a great idea.

Is there a opinion out there about this?

Fredrik Jonsson
ELLEMTEL





Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 13 Jul 1994 09:35:30 GMT
Raw View
konfjo@eua.ericsson.se (Fredrik Jonsson) wrote:
> Why shouldn't virtual members in a base class be forced by the compiler to be
                                                   ^^^^^^
> declared as virtuals when they are overloaded in a derived class? ...
...
> Is there a opinion out there about this?
>
> Fredrik Jonsson
> ELLEMTEL

IMHO this is not needed in general. Everything work fine as long you
are using these methods on objects. (I.e. no references, no pointers,
then static type == dynamic type). Virtual methods create an overhead.
The (successfull) C++ philosphy is not to produce overhead that is not
realy needed. So I am afraid that your suggestion woun't be accepted
as standard ISO-C++.
If a compiler forces you to do something (i.e. if you don't it's an ERROR)
that is not required by the standard this compiler is not standard-compliant.
But I learned in this thread every compiler is free to produce the
warnings it wants.

I agree that it is a good idea if the compiler issues a WARNING:
(a) when one tries to call a non-virtual method of a reference/pointer-object
when there exists a derived class where this method is redefined.
[ I know that the compiler compiling one translation-unit does not
know all classes that (will) 'exist' (in a programm), so this check
is incomplete. But in many cases it would help.]

(b) or additially to (a) when an object is passed by reference/pointer
as actual parameter of a function. Then there is the chance that
within this function (that might be in a translation-unit that does
not know that there is a derived class) a method is called that
was redefined in the derived class.
[ This check is more restrictive but I think it is complete. ]


Ulf Schuenemann

--------------------------------------------------------------------
Ulf Sch   nemann
Institut f   r Informatik, Technische Universit   t M   nchen.
email: schuenem@informatik.tu-muenchen.de