Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2008-07-16 04:13:14


Daryle Walker wrote:

> How strict is the requirement that a base class has to be
> polymorphically connected to the derived class?

It's a fundamental requirement. Without a vtable it just
won't work.

> I have a class that
> derives from a class template that uses the curiously-recurring
> template pattern. Such base classes are usually without any kind of
> virtual stuff, to allow inlining of parent methods and avoid the hit
> virtual dispatch.

no problem. virtual dispatch only costs you time if you use it.
It does cost a little space for the vtable - but with only one
virtual function that shouldn't be too much.

There are other options to look at. maybe you don't want to
serialize through a base class pointer. Perhaps serializing
something like boost::variant - (in the library) might be
a better choice.

your example below doesn't need to serialize through a pointer
to the base class. Just use the BASE_OBJECT macro or
type to invoke the base class serializaton.

Robert Ramey

>
> //==============================================
> template < class Derived >
> class counter_base
> {
> public:
> void count() { ++counter; }
> int current_count() const { return counter; }
> protected:
> counter_base() : counter( 0 ) {}
> private:
> int counter;
> friend class boost::serialization::access;
> template < class Archive >
> void serialize( Archive &ar, unsigned const file_version )
> { ar & counter; }
> };
>
> class my_class
>> private counter_base<my_class>
> {
> typedef counter_base<my_class> base_type;
> typedef my_class self_type;
> public:
> explicit my_class( double factor = 1.0 ) : factor( factor ) {}
> double next() { count(); return factor * current_count(); }
> private:
> double factor;
> friend class boost::serialization::access;
> template < class Archive >
> void serialize( Archive &ar, unsigned const file_version )
> {
> ar & ???; // what goes here for base_type (or base_type::counter)?
ar & BASE_OBJECT(counter_base<my_class>, this); // I forget the exact
syntax
> ar & factor;
> }
> };
> //==============================================


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net