-1

I know this question has been asked previously; however, I'm not understanding the solutions. I'm trying to create a subclass to std::vector, it's able to inherate member functions (such as push_back), but not operators (such as =). From this example it seems like it should just happen automatically... is the vector class different?

#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;
}
user1543042
  • 3,422
  • 1
  • 17
  • 31
  • Your question is a duplicate and the answer is: https://stackoverflow.com/a/9161569/4784683 – Bob Sep 22 '18 at 03:53

1 Answers1

2

you can use using to introduce base class's operator=

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

you probably also need using std::vector<T>::vector; to introduce it's constructors

apple apple
  • 10,292
  • 2
  • 16
  • 36
  • Why isn’t it inherited? – Bob Sep 22 '18 at 03:49
  • 1
    @Adrian because it is not supposed to? you class has it's own `operator=`, which has signature `operator=(const FortranArray1& another)` and would (effectively) call base class's `operator=`. but it does not mean your class would have a `operator=(const base& another)` – apple apple Sep 22 '18 at 03:54