Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

stdvector

Consider:

/* 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?

like image 508
ash Avatar asked Dec 28 '11 18:12

ash


People also ask

Do you need to initialize vectors in C++?

The vector in C++ stores the reference of the objects and not the data directly. These objects can be of any data type like integer, char, string, etc. Unlike static containers like an array, a vector does not need a size to be initialized with.

How do you initialize a vector in a Class C++?

How to Initialize a Vector in C++ Using the push_back() Method. push_back() is one out of the many methods you can use to interact with vectors in C++. It takes in the new item to be passed in as a parameter. This allows us to push new items to the last index of a vector .

What is the correct way to initialize vector in C?

Begin Declare v of vector type. Call push_back() function to insert values into vector v.

Can C use vector?

You can't. By definition, C knows nothing of any of the required components of a std::vector , including, but not limited to: C does not have namespaces, so it can't understand the std namespace. C does not have templates, so it can't understand the std::vector<T> type.


4 Answers

Because C++ is not 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.

new std::vector<my_obj*>()

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

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.

like image 139
Nicol Bolas Avatar answered Nov 02 '22 23:11

Nicol Bolas


Because bar contains a std::vector, not a std::vector *.

It's really no different to something like this:

class bar
{
    int foo;  // No need to create a "new int"
};
like image 27
Oliver Charlesworth Avatar answered Nov 02 '22 23:11

Oliver Charlesworth


Because foo is an object, not a pointer.

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

new returns a pointer:

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

PS. Your 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). I.e., 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
like image 27
Martin York Avatar answered Nov 03 '22 00:11

Martin York


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.

like image 42
ScarletAmaranth Avatar answered Nov 02 '22 23:11

ScarletAmaranth