我有一个 pointAccumulator 的抽象基类。这个抽象基础将填充方法,例如返回所有点的平均值的函数。这两个类的示例如下所示:
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_;
};
正如您在代码中看到的,我还想使用 boost 序列化来保存这些类型。现在使用工厂模式,我相信您可以像这样调用 pointAccumulator 类型:
lala *a1 = new lalaLower();
我的问题是,如果我以这种方式调用模板化序列化方法,则将无法访问它。另外,我不能在抽象类中使用模板类,因为 c++ 不允许这样做。有办法解决这个问题吗?
Edit:
我已经考虑过使用非侵入式的序列化方法,但这需要 height_ 是公开的,这并不理想,也不是良好的编程风格。我认为使用友元类或函数的方法可能可以渗透类并访问变量,同时仍然保持基类抽象?谁能解释一下这是如何工作的?
我认为使用友元类或函数是一个很好的解决方案,您可以添加新类,例如 Serializor
这是友元函数的示例
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& );
};
参考http://msdn.microsoft.com/en-us/library/ahhw8bzz.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)