带有模板成员变量的 C++ 类
我正在尝试解决一个编程问题,该问题由一个包含多个参数的对象(称为图)组成.每个参数(Parameter 类)可以是以下几种类型之一:int、double、complex、string - 仅举几例.
I am trying to solve a programming problem that consists of an object (call it Diagram), that contains several parameters. Each parameter (the Parameter class) can be one of several types: int, double, complex, string - to name a few.
所以我的第一反应是将我的 Diagram 类定义为具有模板参数的向量,看起来像这样.
So my first instinct was to define my Diagram class as having a vector of template parameters, which would look like this.
class Diagram
{
private:
std::vector<Parameter<T> > v;
};
这不能编译,我明白为什么.因此,基于此页面上的建议 如何声明属于类中任何类型对象的数据成员,我将代码修改为:
This doesn't compile, and I understand why. So, based on the recommendations on this page How to declare data members that are objects of any type in a class, I modified my code to look like:
class ParameterBase
{
public:
virtual void setValue() = 0;
virtual ~ParameterBase() { }
};
template <typename T>
class Parameter : public ParameterBase
{
public:
void setValue() // I want this to be
// void setValue(const T & val)
{
// I want this to be
// value = val;
}
private:
T value;
};
class Diagram
{
public:
std::vector<ParameterBase *> v;
int type;
};
我无法弄清楚如何使用适当的模板化参数调用 setValue 函数.在 ParameterBase 抽象基类中不可能有模板化参数.非常感谢任何帮助.
I'm having trouble figuring out how to call the setValue function with an appropriate templated parameter. It is not possible to have a templated parameter in the ParameterBase abstract base class. Any help is greatly appreciated.
附言我没有使用 boost::any 的灵活性.
P.S. I don't have the flexibility to use boost::any.
推荐答案
你们已经很接近了.我添加了一些,因为它们很方便
You got very close. I added a few bits because they're handy
class ParameterBase
{
public:
virtual ~ParameterBase() {}
template<class T> const T& get() const; //to be implimented after Parameter
template<class T, class U> void setValue(const U& rhs); //to be implimented after Parameter
};
template <typename T>
class Parameter : public ParameterBase
{
public:
Parameter(const T& rhs) :value(rhs) {}
const T& get() const {return value;}
void setValue(const T& rhs) {value=rhs;}
private:
T value;
};
//Here's the trick: dynamic_cast rather than virtual
template<class T> const T& ParameterBase::get() const
{ return dynamic_cast<const Parameter<T>&>(*this).get(); }
template<class T, class U> void ParameterBase::setValue(const U& rhs)
{ return dynamic_cast<Parameter<T>&>(*this).setValue(rhs); }
class Diagram
{
public:
std::vector<ParameterBase*> v;
int type;
};
然后图表可以做这样的事情:
Diagram can then do stuff like these:
Parameter<std::string> p1("Hello");
v.push_back(&p1);
std::cout << v[0]->get<std::string>(); //read the string
v[0]->set<std::string>("BANANA"); //set the string to something else
v[0]->get<int>(); //throws a std::bad_cast exception
看起来您的意图是将拥有资源的指针存储在向量中.如果是这样,请注意使 Diagram
具有正确的析构函数,并使其不可复制构造,不可复制赋值.
It looks like your intent is to store resource-owning pointers in the vector. If so, be careful to make Diagram
have the correct destructor, and make it non-copy-constructable, and non-copy-assignable.
相关文章