简体   繁体   English

初始化列表问题,重点是std向量

[英]initialization list question, with emphasis on std vector

newbie here, in order to help sort out problems in my C++ project, I am compiling with the -Weffc++ option in g++. 新手在这里,为了帮助解决我的C ++项目中的问题,我正在使用g ++中的-Weffc ++选项进行编译。

It's giving me hundreds of warnings about "warning ... should be initialized in the member initialization list" 它给了我数百个关于“警告......应该在成员初始化列表中初始化”的警告

Is this really necessary seeing as it will a) take a bit of time to write them all in, and b) make make things messier when I was happy initialising variables when they are need only. 这真的是必要的,因为它会a)花一点时间把它们全部写进来,并且b)当我很高兴在只需要时初始化变量时,让事情变得更加混乱。

But more specifically, how do I initialise a std::vector? 但更具体地说,我如何初始化std :: vector?

Is this the way, ie just putting a 0 in brackets? 是这样的,即只是在括号中加0? My header: 我的标题:

class Blade;
class Postpro {
   public:
     Postpro(string fName) : theFileName(fName),
                             blade(NULL),
                             sizes(10),
                             t(std::vector<double>(0){};
     ~Postpro(){};
     void test();

   private:
     string theFileName;
     Blade* blade;
     int sizes;
     std::vector<double> t;
}

And then in the cpp I just resize the vector if I want to use it? 然后在cpp我只是调整矢量大小,如果我想使用它?

void Postpro::test() {
    blade = new Blade;
    t.resize(37);
}

Thanks 谢谢

It does not take that much time to type, because you have typed too much: 打字不需要太多时间,因为你打字太多了:

Instead of 代替

t(std::vector<double>(0))

you could write 你可以写

t(0)

which should be the same as 它应该是相同的

t()

The warning might be a bit too pedantic. 警告可能有点过于迂腐。 Classes with a default constructor will be automatically initialized anyway. 无论如何,具有默认构造函数的类将自动初始化。 A warning flag to alert you only of uninitialized POD members would seem more valuable (otherwise you get too much noise). 警告标志仅提醒您未初始化的POD成员似乎更有价值(否则您会得到太多噪音)。

May-be the use is that it shows the reader that you indeed meant to use the default constructor to initialize that member. 可能的用途是它向读者显示您确实打算使用默认构造函数来初始化该成员。

Zero might be a valid value for a vector, but I have never seen that. 可能是矢量的有效值,但我从未见过它。

You really don't have to give any value at all, since vector has a default constructor. 你真的不必提供任何值,因为vector有一个默认的构造函数。 Just a t() would do. 只是一个t()会做。

And the warning is just a warning, the vector's default constructor will be called anyway. 警告只是一个警告,无论如何都会调用vector的默认构造函数。

You can do this the way you did it, but there is no point -- the default constructor does exactly the same job. 您可以按照这样做的方式执行此操作,但没有意义 - 默认构造函数执行完全相同的工作。 It is better to use initializer lists, but if it leads to unreadable or unnecessarily complicated code, it's perfectly fine to initialize (to be more precise, reset them from their default values) the member variables in the constructor body. 最好使用初始化列表,但如果它导致不可读或不必要的复杂代码,那么初始化(更准确地说,将它们从默认值重置)构造函数体中的成员变量是完全没问题的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM