When compiling the following code:
foo.h:
#include <memory>
#include <vector>
struct external_class;
struct A {
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 to initialize via default constructor
b(std::make_shared<B>()) // <- does not involve bad_vector::ctor -> warning
{ }
int main() { A a; }
.. with the -Weffc++
flag (gcc) I get the warning
foo.cpp:9:12: warning: ‘A::B::bad_vector’ should be initialized in the member initialization list [-Weffc++]
struct B {
^
Which is totally clear to me but I wonder how I should get rid of it.
For dependency reasons I need the forward declaration for external_class
, so in-class-initialization is not possible. I could provide a constructor for A::B
and implement it inside foo.cpp
but I still hope there is a very short way to initialize A::b
by providing an initializer for A::B::bad_vector
(similar to A::good_vector
).
Is that right? What's the syntax (and should I have googled for to find the solution?) Or do I have to provide a constructor for B
?