Reputation: 485
I have an understanding problem for the Weffc++
warning.
main.cpp: In constructor ‘B::B()’: main.cpp:13:1: warning: ‘B::a’ should be initialized in the member initialization list [-Weffc++] B(){} ^
#include <iostream>
class A
{
public:
A() {}
~A() {}
void test() { std::cout << "Hello world\n"; }
};
class B
{
public:
B() {}
~B() {}
A a;
};
int main()
{
B b;
b.a.test();
return 1;
}
If I write or use initializer list I have no problem.
A a = {};
I thought the default constructor will called automatically? Why I have to use him explicit ?
Upvotes: 2
Views: 1027
Reputation: 385098
I thought the default constructor will called automatically?
It will.
Why I have to use him explicit ?
You don't.
I have an understanding problem for the Weffc++ warning
It's just that. A warning. If we look in the documentation, we find that -Weffc++
represents a style guide, nothing more. It's entirely up to you as to whether or not you wish to follow Meyers's style.
If you did want to follow the suggestion, you could do so like this:
class B
{
public:
B() : a() {} // <-- a() in the ctor-initialiser
~B() {}
A a;
};
… or in the way you already showed.
Frankly, I wouldn't bother. I don't use the -Weffc++
setting.
Upvotes: 6