【发布时间】:2021-11-06 15:30:28
【问题描述】:
在下面的代码中,我创建了一个继承自标准向量类的向量类。我很困惑为什么在 main 中,当我包含以下行时,我的 Vector 对象不调用派生类构造函数
using vector<T>::vector;
但是当我不包含它时,它会按预期调用我的派生类构造函数。这是什么原因?我无法用玩具代码复制问题,因此非常感谢。
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Vector : public vector<T>
{
vector <T> x;
//using vector<T>::vector;
public:
Vector(vector <T> x)
{
this->x = x;
for (auto i: this->x)
cout << i << " ";
}
};
int main() {
Vector <int> x({1,2,3,4,5});
return 0;
}
【问题讨论】:
-
Re:“从标准向量类继承”——这通常是个坏主意。
std::vector并非旨在用作基类。
标签: c++ inheritance vector constructor