C++ vector of objects vs. vector of pointers to objects

前端 未结 6 580
礼貌的吻别
礼貌的吻别 2020-12-07 12:00

I am writing an application using openFrameworks, but my question is not specific to just oF; rather, it is a general question about C++ vectors in general.

I wanted

6条回答
  •  一个人的身影
    2020-12-07 12:08

    vector addition and internal housekeeping use copies of the original object - if taking a copy is very expensive or impossible, then using a pointer is preferable.

    If you make the vector member a pointer, use a smart pointer to simplify your code and minimize the risk of leaks.

    Maybe your class does not do proper (ie. deep) copy construction/assignment? If so, pointers would work but not object instances as the vector member.

提交回复
热议问题