1
我有下面的類代碼生成wreorder警告
#include <string>
class A {
protected:
std::string m1;
int port;
public:
std::string m2;
A(std::string,std::string,int);
};
A::A(std::string _m1,std::string _m2,int _port) : m1(_m1),m2(_m2),port(_port){
}
int main(int argc, char *argv[]){
A("x","y",argc);
}
當用gcc ARM編譯5.40和-Wreorder
它輸出
a.cpp: In constructor ‘A::A(std::__cxx11::string, std::__cxx11::string, int)’:
a.cpp:9:16: warning: ‘A::m2’ will be initialized after [-Wreorder]
std::string m2;
^
a.cpp:6:8: warning: ‘int A::port’ [-Wreorder]
int port;
^
a.cpp:15:1: warning: when initialized here [-Wreorder]
A::A(std::string _m1,std::string _m2,int _port) : m1(_m1),m2(_m2),port(_port){
^
爲什麼會產生警告?
將
m2
和port
具有在main
中指定的默認值或值嗎?爲什麼它不會發生在
m1
?這是一個正確的方式來初始化成員變量?
你是對的,謝謝。 – panitaxx