C++ 继承运算符=

C++ Inheritance operator=

我知道之前有人问过这个问题;但是,我不了解解决方案。我正在尝试创建一个子 class 到 std::vector,它能够继承成员函数(例如 push_back),但不能继承运算符(例如 =)。 从 this example 看来它应该自动发生...向量 class 不同吗?

#include <iostream>
#include <vector>
using namespace std;

template <class T>
class FortranArray1 : public std::vector<T> {
        public:
        T & operator()(int row)
        {
                return (*this)[row-1];
        }

};

int main()
{
        vector <double> y;
        y = {3};        // works
        vector <double> z = y; //works

        FortranArray1<double> x;
        x = {3};        // doesn't work
        x.push_back(3); // works
        cout << y[0] << x[0] << ", " << x(1) ;

        return 0;
}

你可以使用using来引入基础class的operator=

template <class T>
class FortranArray1 : public std::vector<T> {
   public:
   using std::vector<T>::operator=;
   T & operator()(int row){return (*this)[row-1];}
};

你可能还需要using std::vector<T>::vector;来介绍它的构造函数