What is the correct way to initialize the vector member variable of the class?

What is the correct way to initialize the vector member variable of the class? Do we have to initialize it at all?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Method one
class ClassName
{
public:
    ClassName() : m_vecInts() {}

private:
    std::vector<int> m_vecInts;
}

// Method two
class ClassName
{
public:
    ClassName() {} // do nothing

private:
    std::vector<int> m_vecInts;
}



I have an answer from internet: I hope there is someone could help me to understand this:)

Default initialization is performed in three situations:

1 when a variable with automatic storage duration is declared with no initializer
2 when an object with dynamic storage duration is created by a new-expression without an initializer
3 when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.

The effects of default initialization are:

1 If T is a class type, the default constructor is called to provide the initial value for the new object.
2 If T is an array type, every element of the array is default-initialized.
3 Otherwise, nothing is done.

Since std::vector is a class type its default constructor is called. So the manual initialization isn't needed.
No initialisation needed. It's a fully formed size zero vector, ready to go.
Topic archived. No new replies allowed.