且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C ++中从模板基类的构造函数调用模板超类的构造函数?

更新时间:2022-01-23 19:43:19

使用

template <class T> IntArray<T>::IntArray(T s) throw() : Array<T>(s) {}
                                                        //   ^^^ Use <T>

更重要的是,将实现也放在.h文件中.

More importantly, put the implemetation also in the .h file.

请参见为什么只能在头文件?.

我注意到的其他问题

  • 您使用 T s 来确定大小没有意义. std :: size_t s 更有意义.
  • IntArray 是类模板没有任何意义.使用它对我来说更有意义:

  • It does not make sense that you are using T s for size. std::size_t s makes more sense.
  • It does not make sense that IntArray is a class template. It makes more sense to me to use:

class IntArray : public Array<int> { ... };