Destructor is called when I push_back to the vector

前端 未结 4 575
青春惊慌失措
青春惊慌失措 2020-12-19 18:38

I have this class definition:

class FlashStream
{
public:
    explicit FlashStream(const char * url, vector * headers, vector * dat         


        
4条回答
  •  心在旅途
    2020-12-19 18:57

    This statement:

    _streams.push_back(FlashStream(url, headers, data, _npp.ndata, notifyData, lastModified));
    

    is equivalent to:

    {
        FlashStream temp(url, headers, data, _npp.ndata, notifyData, lastModified);
        _streams.push_back(temp);
        // temp gets destroyed here
    }
    

    so, you create a temporary FlashStream object that is copied into the vector, then destructed afterwards. You can avoid this by using emplace_back() in C++11:

    _streams.emplace_back(url, headers, data, _npp.ndata, notifyData, lastModified);
    

提交回复
热议问题