Skip to main content

Posts

Showing posts from November, 2009

C++ initializer list order in a constructor matters?

This is a computer related article, and about THE hardest language, C++. So, I think the audience is again limited. gcc's -Wall option sometimes gives a warning as the order of initializer list in the constructor is wrong. For example, % g++ -Wall initializer_list.cc initializer_list.cc: In constructor 'InitializerListTest::InitializerListTest()': initializer_list.cc:30: warning: 'InitializerListTest::d_j' will be initialized after initializer_list.cc:29: warning: 'int InitializerListTest::d_i' initializer_list.cc:7: warning: when initialized here. This warning said that the order of initializer list in the constructor and the member variables declaration order are different. The following code is such an example. --- #include class InitializerListTest { public: /// constructor InitializerListTest() : d_j(123), // j = 123; d_i(d_j - 1) // i = j - 1; { // empty } /// print out void p