Why doesn't C++ require a “new” statement to initialize std::vector?

前端 未结 7 1214
执笔经年
执笔经年 2021-02-01 13:42
/* bar.h */
class bar{
    /* standard stuff omitted */
    std::vector foo;
};

/* bar.cpp */
bar::bar(){ 
    // foo = new std::vector();         


        
相关标签:
7条回答
  • 2021-02-01 14:39

    You do not need to use new on foo, since foo is a vector, not a pointer to a vector (i.e. std::vector<my_obj*> *foo).

    If you are coming from Java or C#, you may want to consider using std::vector<my_obj> (a vector of objects) instead of a vector of pointers. It really depends on what you want to do.

    0 讨论(0)
提交回复
热议问题