Re: Is finalizing a virtual method possible in C++?
On Feb 22, 10:15 pm, "Bo Persson" <b...@gmb.dk> wrote:
Marcel M=FCller wrote:
[...]
In Java you have all functions made virtual by default.
Sometimes you might want to turn that off.
In C++, functions are only virtual if you explicitly ask for
it. There should be no need to turn if off again - just don't
make them virtual in the first place.
In C++, you might, in a derived class, want to declare that a
virtual function should not be overridden. Consider, for
example, a case where you use a class using the template method
pattern to implement an interface. In that class, you override
the implementations of the (pure) virtual functions in the
interface, but you do not what classes which derive from your
class to override your overrides. In such case, final would be
useful (although in practice, I've not found such accidental
overrides to be a real problem).
Conceptually, there are two orthogonal concepts: overrides, and
can be overridden. That makes four combinations:
doesn't override, cannot be overridden
doesn't override, can be overridden
overrides, cannot be overridden
overrides, can be overridden
In C++, you can't express the third; in Java, you can't express
the first two (for anything but private---for private functions,
you can't express the last three). Ideally, I'd like something
like:
/* no modifier */
virtual
overrides
final
to distinguish all four cases. But as I said, in practice, it's
not enough of a problem to warrent changing the language
(especially given the amount of code this change would break).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34