忽略特定代码的weffc++初始化列表顺序警告



我有以下代码:

class Base {
// Some code
Base(int y) {}
}
class Derived : Base {
int test;
Derived(int x);
}
Derived::Derived(int x) : Base(x)     {
// Some code and calculation to generate vale of test
test = val;
}

我正在使用标志"-Weffc++"进行编译。我收到警告"测试需要在初始化列表中初始化">
但我不能这样做,因为我需要做一些计算来生成测试值。

所以为了避免这个错误,我确实尝试了"-Wno reorder"标志,但它不起作用。我也不喜欢它,因为它会对所有代码禁用这个警告,我只想对这个特定的情况禁用这个警告。

我还使用了位于cpp文件中构造函数之前的"#pragma GCC diagnostic ignore-Weffc++"来完成它,它确实起到了作用。但我需要将pragma添加到我希望避免此警告的所有构造函数中。

但是有没有更好的方法可以避免特定代码的Weffc++初始化列表顺序警告。或者有办法解决这个警告吗?

Derived::Derived(int x) : Base{ x }, test{} // shut up the compiler.
{
// Some code and calculation to generate vale of test
test = val;
}

最新更新