Deny std::vector from deleting its data

后端 未结 9 904
天涯浪人
天涯浪人 2021-01-17 11:03

I have the following case:

T* get_somthing(){
    std::vector vec; //T is trivally-copyable
    //fill vec
    T* temp = new T[vec.size()];
    memc         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 11:50

    Edit: This idea doesn't work because there is no way to prevent the implicit call to the destructors of base classes (thanks, molbdnilo). That they are called is, if I think of it, a good thing.


    I was not entirely sure whether this was viable (and curious as to what others say), but would it be possible to inherit from vector and override its destructor (to do nothing)? Even if ~vector() is not virtual (is there a requirement in the standard for being or not being virtual?) this should work as long as you explicitly use your type.

    By inheriting you would retain all the benefits, in particular the memory management -- except for the final bit (which you don't want).

提交回复
热议问题