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

前端 未结 7 1376
执笔经年
执笔经年 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 *foo).

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

提交回复
热议问题