虚函数和模板冲突 [英] Virtual functions and template clash
问题描述
我有一个pointAcculator的抽象基类。这个抽象基础将使用诸如返回所有点的平均值的函数的方法来填充。这两个类的示例如下所示:
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&高度
}
protected:
std :: deque< double>高度
};
正如你可以看到的代码中,我也想使用boost序列化来保存这些类型。现在使用工厂模式我相信你调用pointAccumulator类型像这样:
lala * a1 = new lalaLower
我的问题是模板序列化方法将无法访问,如果我这样调用。我也不能在抽象类中有模板类,因为这是c ++不允许的。是否有办法解决这个问题?
编辑:
序列化,但这需要高度是公共的,这是不理想的,也不是好的编程风格。我认为一个使用友类或函数的方法可能渗透到访问变量的类中,同时仍然保持基类的抽象?
我认为使用友元类或函数是一个很好的解决方案,你可以添加新的类像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>高度
friend int Serializor :: Func1(Serializor&);
};
请参阅 http://msdn.microsoft.com/en-us/library/ahhw8bzz.aspx
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?
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
这篇关于虚函数和模板冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!