【发布时间】:2013-11-30 19:43:59
【问题描述】:
我在这个例子中最抽象的模板化类有问题。
所以对于具有以下形式的类
template <typename T, template <typename T> class MyFunctor>
class MyMainClass
{
MyFunctor<T> myInstance;
public:
setConfigOfMyFunctor<ConfigClass>(const ConfigClass& cfg); //problem is here. How can I write this?
}
我定义了 FunctorClass,我将把它用作 MyMainClass 中的第二个模板参数
template <typename T>
class FunctorClass
{
public:
ConfigClass<T> cfg;
int a;
int b;
int c;
}
我的配置类在哪里
template <Typename T>
class ConfigClass
{
T cfgval1;
T cfgval2;
public:
void setVals(int cfgVal1, ...);
//.....
}
如果我创建这个类的对象
int main()
{
typedef double T;
MyMainClass<T,FunctorClass> classy;
ConfigClass<T> config;
config.setVals(1, 2, ...);
//so I'm looking for something like the following line (taken from first declaration)
classy.setConfigOfMyFunctor<ConfigClass>(config); //this is supposed to copy the object config to the FunctorClass's object in MyMainClass.
}
简而言之,我希望将对象 config 复制到 MyMainClass::FunctorClass::cfg
这可能吗?
如果您需要有关该问题的更多信息,请告诉我。
感谢您的努力。
【问题讨论】:
-
例如,您可以在
FunctorClass中提供typedef,然后将函数编写为void setConfigOfMyFunctor(typename MyFunctor<T>::ConfigClass const& cfg);,或者使用函数模板template<class U> void setConfigOfMyFunctor(U const&);
标签: c++ class templates template-templates