Is it possible to create a vector of pointers?

后端 未结 6 2342
情歌与酒
情歌与酒 2020-12-15 01:27

Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin()

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 01:38

    vector cvect is not a vector of pointers. It is a vector of objects of type c. You want vector cvect. and the you probably want:

    cvect.push_back( new c );
    

    And then, given an iterator, you want something like:

    (*it)->func();
    

    Of course, it's quite probable you didn't want a vector of pointers in the first place...

提交回复
热议问题