c++ - syntax for initializing nested member in constructor? -


when compiling following code:

foo.h:

#include <memory> #include <vector>  struct external_class;  struct {     struct b {         std::vector<external_class> bad_vector;     };     std::vector<external_class> good_vector;     std::shared_ptr<b> b;     a(); }; 

foo.cpp:

#include "foo.h":  struct external_class {};     // implement external_class  a::a() :      good_vector(),            // <- easy initialize via default constructor     b(std::make_shared<b>())  // <- not involve bad_vector::ctor -> warning { }  int main() { a; } 

.. -weffc++ flag (gcc) warning

foo.cpp:9:12: warning: ‘a::b::bad_vector’ should initialized in member initialization list [-weffc++]      struct b {             ^ 

which totally clear me wonder how should rid of it.

for dependency reasons need forward declaration external_class, in-class-initialization not possible. provide constructor a::b , implement inside foo.cpp still hope there short way initialize a::b providing initializer a::b::bad_vector (similar a::good_vector).

is right? what's syntax (and should have googled find solution?) or have provide constructor b?

your code fine.

the warning b not having constructor explicitly default-initializes bad_vector. default constructor b default-initializes bad_vector. b() = default; doesn't silence warning, have write out b() : bad_vector() { }. me suggests -weffc++ may obsolete in c++11.


Comments