問題描述
我正在嘗試解決一個編程問題,該問題由一個包含多個參數的對象(稱為圖)組成.每個參數(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.
這篇關于帶有模板成員變量的 C++ 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!