【问题标题】:What is the correct syntax to call the constructor of the parent class in a derived class with templates in c++?在 C++ 中使用模板调用派生类中父类的构造函数的正确语法是什么?
【发布时间】: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。你说得有道理。我有一个坏习惯,就是为我的班级挑选真正的大牌。

标签: c++ templates


【解决方案1】:

唯一可以调用基类构造函数的地方是constructor initializer list

genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Paramaters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
        :genericalMethodClassWithInternalParameterClass<CTHIS, myNUpleType<T>>(&_funcVersionWith2Paramaters, _thisPar, _fixedParam)
            {
                funcVersionWith2Paramaters = _funcVersionWith2Paramaters;   
            }

Example on godbolt.

请注意,类的基类部分始终必须在类本身之前构造。所以不能先初始化派生类的成员,然后再去构造基类。

【讨论】:

  • "请注意,类的基类部分始终必须在类 itel 之前构造"。这是带有模板的类的规则吗?因为我一直把它用于常规课程。从 myDerivedClass::myDerivedClass 内部调用 parentClass::parentClass() 在我需要在调用 parentClass 构造函数之前对某个内部变量执行某些操作的情况下一直对我有用。
  • @AndersonBrasil 不,这是一般规则,也适用于非模板类。请参阅this question,了解为什么您的代码在其他情况下编译以及为什么该代码没有按照您的想法执行。
  • 很好,我学到了一些东西。我想知道到目前为止,我的程序如何在我所有的构造函数都以错误的方式编写时运行良好,哈哈。
【解决方案2】:

简单的方法是为基本类型创建一个 typedef。

using base_type = genericalMethodClassWithInternalParameterClass&lt;CTHIS,myNUpleType>;

那么构造函数就变成了:

genericalMethodClassWithSeveralInternalParametersClass(float (CTHIS::*_funcVersionWith2Paramaters)(float, myNUpleType<T>) const, const CTHIS *_thisPar, myNUpleType<T> _fixedParam)
: base_type(&_funcVersionWith2Parameters, _thisPar, _fixedParam),
funcVersionWith2Paramaters (_funcVersionWith2Paramaters)
            {
             }

【讨论】:

  • 我还没有尝试过,但我想这个技巧可能对从父类调用任何方法很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 2020-11-28
  • 2021-04-20
相关资源
最近更新 更多