gcc warning:‘Test::b’will be initialized after

来源:互联网 发布:json 汉字 编辑:程序博客网 时间:2024/05/17 09:44

  如标题,gcc默认编译如下代码时,会出现下述警告,问题的原因是:cpp文件中的构造函数的成员初始化列表的顺序与其声明顺序不一致。(注意成员a和b的初始化顺序)
解决办法:
1. 使初始化列表的顺序与声明顺序一致。
2. 添加编译选项 -Wno-reorder 。

[root@localhost src]# gcc -Wall -c test.cpptest.h: In constructorTest::Test()’:test.h:6: warning: ‘Test::b’ will be initialized aftertest.h:5: warning:   ‘int Test::a’test.cpp:2: warning:   when initialized here

Test.h

#ifndef TEST_H#define TEST_Hclass Test {    int a;    int b;    int c;    Test();    ~Test();};#endif

Test.cpp

#include "test.h"Test::Test() : b(0), a(0), c(0) {}Test::~Test() {}
阅读全文
0 0
原创粉丝点击