C++ Vector of pointers

后端 未结 4 993

For my latest CS homework, I am required to create a class called Movie which holds title, director, year, rating, actors etc.

Then, I am required to read a file which c

4条回答
  •  一生所求
    2021-01-31 10:02

    By dynamically allocating a Movie object with new Movie(), you get a pointer to the new object. You do not need a second vector for the movies, just store the pointers and you can access them. Like Brian wrote, the vector would be defined as

    std::vector movies

    But be aware that the vector will not delete your objects afterwards, which will result in a memory leak. It probably doesn't matter for your homework, but normally you should delete all pointers when you don't need them anymore.

提交回复
热议问题