我有一个模板类可以(或必须)专门的参数。 我想把我的所有参数放在一个容器中。 如何做到这一点,如果我的参数与不同类型instanciated?C++是否有可能拥有一个具有不同类型参数的专用模板对象的容器?
在类容器,我会希望有不同类型(int,双...)或等同的东西的载体<参数* >这似乎是不可能的。
如果Parameter类是从基类派生的,那么The Container可以声明vect为向量< Base * >。但在这种情况下,我们无法在Container :: foo中做任何具体的操作。
以下是我的源代码示例。我的一个参数是一个与ostream不兼容的QString。
感谢您的意见。
#include <QString>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define P(a) cout << #a << ":" << a << endl
/*
class Base {
};
*/
template<typename T> class Parameter /*: public Base */ {
private:
T val;
public:
void setVal(const T &val) {
this->val = val;
}
const T &getVal() {
return val;
}
string getFoo() {
stringstream s;
s << val;
return s.str();
}
};
template<>
string Parameter<QString>::getFoo() {
stringstream s;
s << val.toStdString();
return s.str();
}
class Container {
public:
void push_back(Parameter *base) {
vect.push_back(base);
}
void foo() {
/* do something with the parameters */
}
private:
vector<Parameter*> vect;
};
int main() {
Parameter<int> pi;
Parameter<QString> ps;
pi.setVal(10);
ps.setVal("QString");
P(pi.getVal());
P(ps.getVal().toStdString());
P(pi.getFoo());
P(ps.getFoo());
Container container;
container.push_back(&pi);
container.push_back(&ps);
}
非常感谢您的意见。我会按照你的意见,并使用boost :: any。 以下是更新版本:
#include <boost/any.hpp>
#include <QString>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define P(a) cout << #a << ":" << a << endl
template<typename T> class Parameter {
private:
T val;
public:
void setVal(const T &val) {
this->val = val;
}
const T &getVal() {
return val;
}
string getFoo() {
stringstream s;
s << val;
return s.str();
}
};
template<>
string Parameter<QString>::getFoo() {
stringstream s;
s << val.toStdString();
return s.str();
}
class Container {
public:
void push_back(boost::any base) {
vect.push_back(base);
}
void foo() {
cout << "do something with the parameters\n";
for (vector<boost::any>::iterator i = vect.begin(); i != vect.end(); ++i) {
boost::any a = (*i);
if (a.type() == typeid(Parameter<int>*)) {
Parameter<int> *ai = boost::any_cast<Parameter<int> *>(a);
cout << ai->getFoo() << endl;
} else if (a.type() == typeid(Parameter<QString>*)) {
Parameter<QString> *aq = boost::any_cast<Parameter<QString> *>(a);
cout << aq->getFoo() << endl;
} else {
cout << "unknown type:" << a.type().name() << endl;
}
}
}
private:
vector<boost::any> vect;
};
int main() {
Parameter<int> pi;
Parameter<QString> ps;
pi.setVal(10);
ps.setVal("QString");
P(pi.getVal());
P(ps.getVal().toStdString());
P(pi.getFoo());
P(ps.getFoo());
Container container;
container.push_back(&pi);
container.push_back(&ps);
container.foo();
}
你是什么意思由Qstring“与ostream不兼容”,它与这个问题有什么关系?你是否想要调用参数 :: getFoo()?你可以提供一个方法让operator <<自己工作,如果它有意义的话。 –
bacar
2012-01-05 20:39:57
你到底想要做什么?如何使用Base不能解决您的问题?这听起来像你想要的东西是通用的多种类型,同时具有一些特定于该类型的功能(“我们无法在Container :: foo中专门做任何事情”) - 我不确定如何在没有向下转换的情况下有效实现这一点。 – bacar 2012-01-05 20:41:51
@bacar模板类中的getFoo()不适用于QString。我们必须使用:s < < val.toStdString()。
– user1132852 2012-01-05 20:48:45