Boost logo

Boost Users :

From: Daryle Walker (darylew_at_[hidden])
Date: 2008-07-16 02:43:03


On Jul 14, 2008, at 5:40 PM, Steve Sanders wrote:

> Thank you, that makes my sample work. These points could be
> clearer in the docs, I would say.
>
>> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
>> bounces_at_[hidden]] On Behalf Of Robert Ramey
>> Sent: Monday, July 14, 2008 2:54 PM
>> To: boost-users_at_[hidden]
>> Subject: Re: [Boost-users] [serialization] Visual Studio 2008 +
>> Boost 1.35:runtime casting not working
>>
>>
>>
>> The base class MUST be polymorphic - that is have at least one
>> virtual function.
>>
>>
>>
>> AND the the pointer to the base class - not the derived class
>> should be serialized.
>>
>>

How strict is the requirement that a base class has to be
polymorphically connected to the derived class? 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.

//==============================================
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 & 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