c++ garbage values in vector of pointer

前端 未结 6 1964
青春惊慌失措
青春惊慌失措 2020-12-22 00:12

When I do:

for(i=0; i

s

6条回答
  •  盖世英雄少女心
    2020-12-22 00:34

    When you use push_back to add an element to a vector, it is copied into the vector. The means that, in the first example, vectorA contains a copy of objectA, and objectA goes out of scope and gets deleted right after the closing brace. This means that the pointer in pvectorA is pointing at at an address that doesn't necessarily contain objectA any more.

    Your second example shouldn't work, because objectA has gone out of scope after the first loop, so I can't help you there.

提交回复
热议问题