Boost logo

Boost Users :

From: Daryle Walker (darylew_at_[hidden])
Date: 2008-07-16 04:54:38


[Sent it through the wrong mail account again!]

On Jul 16, 2008, at 4:13 AM, Robert Ramey wrote:

> 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.

This is only for channeling through a base-class pointer, right?
Just the standard pointer won't check the final type w/o some sort of
polymorphic connection (or brute force w/ static_cast).

>> 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 serialization.

My class is like a function object, and will be used strictly as a
value object. There shouldn't any reason to access a base class,
especially via a pointer. (It uses the curiously-recursive template
pattern, so my class is the sole derived class of the base class'
particular instantiation.) The base class adds implementations of
common variant operations, with the derived class supplying the core
routines.

So I'll just define serialization in every class in the hierarchy,
and make sure each non-root class calls "ar &
boost::serialization::base_object<BASE_TYPE>(*this)". Can I use a
typedef for the base type, or do I have to use the original name
expression?

Does this all work for any kind of base: non-polymorphic,
polymorphic, virtual inheritance, and/or multiple inheritance?

>> //==============================================
>> 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;
>> }
>> };
>> //==============================================

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

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