调整矢量向量的大小会导致seg错误

时间:2016-02-17 04:41:40

标签: c++ c++11

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";
    }
};

上面的代码导致了一个seg错误,我无法弄清楚出了什么问题?

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

1 个答案:

答案 0 :(得分:5)

static_cast<super>(*this)创建临时和切片*this

您想要static_cast<super&>(*this)