Correct way to initialize vector member variable

前端 未结 4 623
忘了有多久
忘了有多久 2021-01-30 02:16
// Method One
class ClassName
{
public:
    ClassName() : m_vecInts() {}

private:
    std::vector m_vecInts;
}

// Method Two
class ClassName
{
public:
    C         


        
4条回答
  •  温柔的废话
    2021-01-30 03:01

    It depends. If you want a size 0 vector, then you don't have to do anything. If you wanted, say, a size N vector fill of 42s then use the constructor initializer lists:

    ClassName() : m_vecInts(N, 42) {}
    

提交回复
热议问题