Victor said:
Huh? Since when does inheriting require virtual functions? Why does
it always necessary to assume the worst when giving out language advice?
I don't see much sense in this explanation, sorry. It seems to me that
many a recommendation of this sort have the same basis as McDonald's
warning about the temperature of the contents of their coffee cups.
Well you know why they did that don't you? They got sued for having
their coffee at such a high temperature that it caused 3rd degree burns
when spilled. The judge found that keeping it at such a high
temperature was negligent and found for the plaintifs. That warning is
McDonald's way of saying, "**** you, idiots." It's debatable...it's
pretty dumb to expect that hot coffee isn't hot but then you don't
expect it to remove flesh either. It does seem rather negligent and
underhanded to hand someone coffee that will burn the flesh off their
bones to someone about to drive off in a car.
We are not bound by the same laws, we should stop making excuses for
the beginners not to learn the damn language. "Oh, don't inherit from
this class, sweetheart, it has no virtual functions, you might not
get what you think you might get..." Bullsh*t. Let them try it. If
it's too hot, they will learn much better than if you keep telling them
not to drink it (usually).
Well, it is good to warn them because the errors that can come about
are rather...interesting, to say the least. I think it sufficient
though to just say that the subclass will not be a polymorphic vector
and leave it at that...in other words, never, ever, pass it to a
function that wants to work on a vector that will call any of the
overridden functions.
Usually though, when people subclass vector it is so they can add
behavior without having to add wrappers for all the functions vector
has, as would be necissary with composition (the more theoretically
sound choice). So usually you're not overriding behavior anyway; when
you do you just have to keep in mind that it isn't polymorphic in any
fassion. Add to that the fact that much of the time vectors are passed
to templated functions so the issue is moot in that regard. In the end
there are a few times when you need to worry about it: when you want to
pass to a function expecting a vector of the type you are containing;
or when you want to pass to a template explicitly expecting a vector
(and then it just won't compile).