Re: Global Dependencies Made Easy
Gerhard Menzl schrieb:
Daniel Kr?gler wrote:
Not 100% equal, but quite good in general. The main disadvantages
of even private inheritance are:
- virtual functions can be overridden in the derived class.
I don't see why this would be a disadvantage.
Depends on the point of view. If I use private inheritance
in the sense of has-a, I might not have considered that I
override a virtual function of that base class, because I
really don't think in terms of is-a in this scenario. Of course
no-body is so irrational and so scatterbrained to overlook
such an obvious point and fall into that trap....
I wanted to demonstrate that private inheritance is
not a 100% equivalent alternative to membership,
sometimes it's better, sometime it's worse. For just
the same reason will a library often not take advantage
of EBCO (empty-base-class-optimization) in it's *public*
classes to prevent unexpected leak-effects. Consider
a std library implementing std::set, where the set
class template would directly inherit from the predicate
(if that is a class).
template <class Key, class Compare, ....>
// Only if is_class<Compare>::value == true:
class set : private Compare {
....
};
Because this predicate can be actually everything,
this is probably an unwise idea. E.g. just by chance the
Compare class is more than a comparator - possibly
because it has another base class with a virtual size() const
function, that is used inside the implementation of
operator() to implement the comparison - Now the
std::set<>::size() silently overrides this virtual member
- shall I speculate more on that?
Fortunately in this case potential problems can be
easily prevented by simply using a wrapper member
instead:
template <class Key, class Compare, ....>
class set {
...
private:
// Only if is_class<Compare>::value == true:
struct Wrapper : Compare {
othermembers;
} membar;
};
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! ]