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

前端 未结 7 1229
执笔经年
执笔经年 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:19

    Because std::vector does that for you :) You don't have a pointer to std::vector, you're simply setting up an object of type std::vector, which internally allocates memory for you.

提交回复
热议问题