Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialization list question, with emphasis on std vector

Tags:

c++

newbie here, in order to help sort out problems in my C++ project, I am compiling with the -Weffc++ option in g++.

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.

But more specifically, how do I initialise a std::vector?

Is this the way, ie just putting a 0 in brackets? 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?

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

Thanks

like image 347
CptLightning Avatar asked Aug 24 '26 18:08

CptLightning


1 Answers

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).

May-be the use is that it shows the reader that you indeed meant to use the default constructor to initialize that member.

like image 165
UncleBens Avatar answered Aug 27 '26 07:08

UncleBens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!