The following code, as far as I can tell, correctly initializes the variables of the derived class B
:
#include <utility>
struct A {
int i;
};
struct B : A {
int j;
explicit B(A&& a) : A(std::move(a)), j{i} { }
};
int main()
{
A a{3};
B b(std::move(a));
return 0;
}
Running cppcheck with --enable=all
gives the warning:
[test.cpp:9]: (warning) Member variable 'A::i' is not initialized in the constructor. Maybe it should be initialized directly in the class A?
Is there a reason for this (false, I think) warning?
Yes, this looks like a false positive. Base class subobjects are initialized before direct member subobjects and
A(std::move(a))
will use the implicit move constructor which initializesthis->i
witha.i
, sothis->i
will be initialized before the initialization ofthis->j
is performed (which readsthis->i
).The argument given to the constructor in
main
is also completely initialized via aggregate initialization, soa.i
's value will not be indeterminate either.