为什么 C++ 不需要“new”语句来初始化 std::vector?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8660025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:56:43  来源:igfitidea点击:

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

c++stlstdvector

提问by ash

/* bar.h */
class bar{
    /* standard stuff omitted */
    std::vector<my_obj*> foo;
};

/* bar.cpp */
bar::bar(){ 
    // foo = new std::vector<my_obj*>(); <-- why don't I need this line??
    foo.push_back(new my_obj());
}

Why does this code work even though we didn't assign foo a new instance of std::vector ?

为什么即使我们没有为 foo 分配 std::vector 的新实例,这段代码仍然有效?

回答by Nicol Bolas

Because C++ is not C#/Java.

因为 C++ 不是 C#/Java。

std::vector<my_obj*> foo;

This is a definition of an object, not a reference as in C#/Java. An object is a living instance of a type.

这是一个对象的定义,而不是 C#/Java 中的引用。对象是类型的活实例。

new std::vector<my_obj*>()

This expression returns a pointer. It returns a std::vector<my_obj*>*, which is notthe same type as foo(the *at the end is what makes them different). foois an object, std::vector<my_obj*>*is a pointer to an object.

此表达式返回一个指针。它返回一个std::vector<my_obj*>*,这是为同一类型foo(与*在到底是什么让他们有所不同)。foo是一个对象,std::vector<my_obj*>*是一个指向对象的指针。

Objects (rather than pointers or references) have specific lifetimes. If you create a pointer to an object with new, the lifetime of the object pointed to will be until you explicitly call delete. If you create an object as a member of another object, then that inner object's lifetime will (more or less) mirror the outer object's lifetime. If you create an object on the stack (a parameter or variable at function scope), then its lifetime is the current scope of that variable name.

对象(而不是指针或引用)具有特定的生命周期。如果您使用 来创建指向对象的指针new,则指向的对象的生命周期将一直持续到您显式调用delete. 如果您创建一个对象作为另一个对象的成员,那么该内部对象的生命周期将(或多或少)反映外部对象的生命周期。如果您在堆栈上创建一个对象(函数范围内的参数或变量),则其生命周期是该变量名称的当前范围。

回答by Oliver Charlesworth

Because barcontains a std::vector, not a std::vector *.

因为bar包含一个std::vector,而不是一个std::vector *

It's really no different to something like this:

与这样的事情真的没有什么不同:

class bar
{
    int foo;  // No need to create a "new int"
};

回答by Martin York

Because foo is an object not a pointer.

因为 foo 是一个对象而不是一个指针。

std::vector<my_obj*>    // This is an object
std::vector<my_obj*> *  // This is a pointer to an object
                    ^^^ // Notice the extra star.

New rerturns a pointer:

New 返回一个指针:

new std::vector<my_obj*>();  // returns std::vector<my_obj*> *

PS. You vector should probably contain objects not pointers.

附注。你的向量应该包含对象而不是指针。

std::vector<my_obj>   foo;
...
foo.push_back(my_obj());

Otherwise you will need to manually delete all the objects in the vector when it goes out of scope (when the containing object is destroyed). ie if you want to keep pointers in your vector you should do one of the following:

否则,当向量超出范围时(当包含对象被销毁时),您将需要手动删除向量中的所有对象。即如果你想在你的向量中保留指针,你应该执行以下操作之一:

// 1. Manually delete all the elements in the vector when the object is destroyed.
~bar::bar()
{
    for(std::vector<my_obj*>::iterator loop = foo.begin(); loop != foo.end(); ++loop)
    {
        delete (*loop);
    }
}

// 2. Use a smart pointer:
std::vector<std::shared_ptr<my_obj> >  foo;

// 3. Use a smart container for pointers
boost::ptr_vector<my_obj>   foo

回答by ScarletAmaranth

Because std::vectordoes 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.

因为std::vector这样做对你来说 :) 你没有指向 的指针std::vector,你只是设置一个 type 的对象std::vector,它在内部为你分配内存。

回答by crashmstr

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

您不需要使用newon foo,因为 foo 是 a vector,而不是指向 a vector(即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.

如果您来自 Java 或 C#,您可能需要考虑使用std::vector<my_obj>(对象向量)而不是指针向量。这真的取决于你想做什么。

回答by AAlkhabbaz

std::vector in this library is not a pointer

此库中的 std::vector 不是指针

回答by Pavan Yalamanchili

std::vector<my_obj *> foois different from std::vector<my_obj *> *foo. The second case will require you to use new while the first wll not.

std::vector<my_obj *> foo不同于std::vector<my_obj *> *foo. 第二种情况将要求您使用 new 而第一种情况则不会。