Kam
Kam

Reputation: 6008

resizing a vector of vectors causes a seg fault

class MyVect:private std::vector<std::vector<std::string> >
{
    typedef std::vector<std::vector<std::string> > super;

public:
///When this is commented out -- No Seg Fault//////////////
    std::vector<std::string>& operator[](int plt)
    {
        return static_cast<super>(*this)[(int)plt];
    }
///////////////////////////////////////////////////////////

    MyVect()
    {
        this->resize(4); // this works fine
        for (int i = 0; i<4;i++)
        {
            (*this)[i].resize(3); // I think this doesn't 
        }
        (*this)[0][0] = "hello";
    }
};

The above code, causes a seg fault and I can't figure out what is wrong?

*** glibc detected *** invalid fastbin entry (free): 0x09267040

Upvotes: 2

Views: 432

Answers (1)

T.C.
T.C.

Reputation: 137425

static_cast<super>(*this) creates a temporary and slices *this.

You want static_cast<super&>(*this).

Upvotes: 6

Related Questions