【发布时间】:2021-07-06 13:46:50
【问题描述】:
我的类需要调用他父类的构造函数。但我对此有疑问,因为它们是模板类。
我的派生类的代码如下:
template <class CTHIS, typename T>
class genericalMethodClassWithSeveralInternalParametersClass:public genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>
{
protected:
float (CTHIS::*funcVersionWith2Parameters)(float, myNUpleType<T>) const;
public:
genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Parameters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
{
funcVersionWith2Parameters = _funcVersionWith2Parameters;
genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>::genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(funcVersionWith2Parameters, _thisPar, _fixedParam); // compiler doesn't accept this line
}
};
构造函数的第二行应该调用父类的构造函数,但是编译器不接受。我应该在里面放什么?
编辑: 我只是在初始化列表中添加了构造函数并且它起作用了:
template <class CTHIS, typename T>
class genericalMethodClassWithSeveralInternalParametersClass:public genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>
{
protected:
float (CTHIS::*funcVersionWith2Parameters)(float, myNUpleType<T>) const;
public:
genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Parameters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam):genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(_funcVersionWith2Parameters, _thisPar, _fixedParam)
{
funcVersionWith2Parameters = _funcVersionWith2Parameters;
}
};
不过,对于未来,我想知道如何在派生类代码中调用父类的构造函数。其实想了想,这个语法问题很可能会出现在任何调用父类(不仅仅是构造函数)的方法中。
【问题讨论】:
-
请在您的代码 sn-ps 中使用较短的名称。
-
对不起,@HolyBlackCat。你说得有道理。我有一个坏习惯,就是为我的班级挑选真正的大牌。