Re: Boost and multi-level inheritance
on Tue Aug 28 2007, Marcin Gil <marcin.gil-AT-NOSPAMgmail.com> wrote:
Hi!
Does it produce much overhead if i.e. I had:
class A: public boost::less_than_comparable<A>;
class B: public A, public boost::less_than_comparable<B>;
class C: public B, public boost::less_than_comparable<C>;
?
What kind of overhead do you anticipate? Some compilers will allocate
a little extra space in B and C to store the empty multiply-inherited
less_than_comparable<B/C> instances.
you can get around that using
class A: public boost::less_than_comparable<A>
{
bool operator<(A const&) const;
};
class B: public boost::less_than_comparable<B,A>;
{
bool operator<(B const&) const;
};
class C: public boost::less_than_comparable<C,B>
{
bool operator<(C const&) const;
};
see http://boost.org/libs/utility/operators.htm#chaining
Let's say that the type A is not directly used and is only
used as a pointer/reference parameter type in other classes, ie.:
class D
{
public:
int fun(const A&);
}
and having:
B b;
C c;
D d;
should work for
d.fun(b);
d.fun(c);
OK...
Also I need this operation:
b < c
to work.
It will "work" by using B::operator<(B const&). Whether or not that's
the function you want it to call, I cannot say.
Moreover, for:
B b1; A& ba1 = b1;
B b2; A& ba2 = b2;
the
ba1 < ba2
should also work with correct operator<().
Which is the correct one? You haven't explained the semantics you
want. If you expect that to call B's operator<, then you'd better
make operator< virtual in A.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]