我目前正在尝试理解g ++在哪些情况下警告未初始化的变量.考虑以下代码:
#include <iostream>
typedef double barType;
struct foo {
barType bar;
};
int main() {
foo f;
std::cout << f.bar << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我像这样编译它我没有得到警告:
$ g++ -O1 -Wall test.cc -o test
Run Code Online (Sandbox Code Playgroud)
但如果我将barType更改为int:
$ g++ -O1 -Wall test.cc -o test
test.cc: In function ‘int main()’:
test.cc:17: warning: ‘f.foo::bar’ is used uninitialized in this function
Run Code Online (Sandbox Code Playgroud)
警告如何取决于类型?在这两种情况下都没有初始化.
我正在使用:
$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
谢谢,
谁
| 归档时间: |
|
| 查看次数: |
1400 次 |
| 最近记录: |