Re: How to call protected pure virtual ancestor funcs from friend function?
On 22 Mai, 14:15, "Siegfried Heintze" <siegfr...@heintze.com> wrote:
- The "abstract" base class misses a virtual d'tor.
Could you elaborate on this? Why would one be necessary here?
It's not strictly required, but it is rather wise
to do so when a public class hierachy exists, which
already uses virtual functions and is used via
dynamic memory:
1) If there exists already one virtual function (as
your pure virtual ones), then the class already is
polymorphic and typical implementations don't introduce
further overhead (because e.g. a v-table already exists).
2) If you forget to make the base class d'tor virtual
and if you create such objects via the free store (that
is via new and his friend), than invoking delete on
the pointer which's static type differs from its
dynamic type causes UB according to 5.3.5/2:
"In the first alternative (delete object), if the static
type of the operand is different from its dynamic type,
the static type shall be a base class of the operand's
dynamic type and the static type shall have a virtual
destructor or the behavior is undefined."
A typical example of such a situation is this one:
ISTACK<int> ps = new STACK<int>(...);
....
delete ps;
This seemingly harmless can totally wreck your
program...
Since normal users of polymorphic classes
would expect to use those classes with dynamic
storage durations, you must make the base class
d'tor virtual to prevent that. Alternatively
you should at least make it's d'tor protected.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]