Search code examples
c++templatesabstract-classboost-serialization

Virtual functions and template clash


I have an abstract base class for a pointAccumulator. This abstract base will be filled out with methods such as a function that returns mean of all the points. An example of these two classes is shown below:

class lala {
public:
    virtual someFunctions = 0;

    virtual bool isEmpty() = 0;
};


class lalaLower : public lala {
public:
    lalaLower(){}
    ~lalaLower(){}


    someFunctions

    template<class Archive> void serialize(Archive & ar, const unsigned int version) {
        ar & heights_;
    }

protected:
    std::deque<double> heights_;
};

As you can see in the code I would also like to use boost serialization in to save these types. Now using a factory pattern i believe that you call the pointAccumulator types like this:

lala *a1 = new lalaLower();

My problem is that the templated serialize method will not be accessible if I call it this way. Also I cannot have the templated class in the abstract class as this is not allowed by c++. Is there a way to get around this?

Edit:

I have considered the non-intrusive method for serialization but that requires heights_ to be public which is not ideal, nor is it good programming style. I thought potentially a method using friend classes or functions could penetrate the class with access to the variables while still keeping the base class abstract? can anyone explain how this would work?


Solution

  • I think using friend classes or functions is a good solution, you could add new class like Serializor

    here is a example of friend function

    class Serializor;
    class meanAccumulator : public pointAccumulator 
    { 
    public:     
    meanAccumulator(){}     
    ~meanAccumulator(){}     
    double getHeight();     
    void addHeight(double Height);     
    void setHeight(double Height);     
    bool isEmpty(){ return heights_.empty(); }      
    
    protected:     std::deque<double> heights_; 
    friend int Serializor::Func1( Serializor& );
    
    };
    

    refer to http://msdn.microsoft.com/en-us/library/ahhw8bzz.aspx