foo.h
#ifndef FOO_H
#define FOO_H
class Foo
{
int fooObj1;
bool fooObj2;
public:
Foo(int input1);
};
#endif
foo.cpp
#include "foo.h"
Foo::Foo(int input1)
{
fooObj1 = input1;
// some code logic to decide the value of fooObj2 (an example)
// so I can't really do member initialization list.
fooObj2 = (fooObj1 % 2 == 0);
}
So I was following a tutorial and they told me to turn on [-Weffc++]
and treat warnings as errors. But when I do that, [-Weffc++]
give a warning: 'Foo::fooObj1' should be initialized in the member initialization list [-Weffc++]
and 'Foo::fooObj2' should be initialized in the member initialization list [-Weffc++]
. But I can't really do member initialization list in this project. So how can I reslove this warning?
Two solutions to get rid of the warning.
Solution 1
Make some static method, say
CalculateFooObj2InitialValue
, and use it in member initialization list.Solution 2
Initialize
fooObj2
with a default, yet not quite meaningful, value in member initialization list. And then calculate a meaningful initial value later and assign.