Vector Iterators Incompatible

后端 未结 10 1016
别那么骄傲
别那么骄傲 2020-12-04 23:54

I have a class with a std::vector data member e.g.

class foo{
public:

const std::vector getVec(){return myVec;} //other stuff omitted

private:
s         


        
10条回答
  •  感情败类
    2020-12-05 00:26

    Another reason why this assert can trigger is if you would allocate "foo" with 'malloc' instead of 'new', effectively skipping the constructor(s).

    It's unlikely to happen to a project developed from scratch in C++, but when converting plain-C code to C++ (replacing a static array[] in some struct with an stl-vector) you might just not realise that dynamic instances of said struct (and the members inside) are not going to have their constructor called - unless you also change 'malloc' to 'new'.

提交回复
热议问题