我的理解是int变量会自动初始化为0;然而,事实并非如此.下面的代码打印一个随机值.
My understanding is that an int variable will be initialized to 0 automatically; however, it is not. The code below prints a random value.
int main ()
{
int a[10];
int i;
cout << i << endl;
for(int i = 0; i < 10; i++)
cout << a[i] << " ";
return 0;
}
如果
MyClass 实例;int a[10] = {}(全部归零)或 int a[10] = {1,2};(除前两项外全部归零:a[0] == 1 和 a[1] == 2)static(无论是在函数内还是在全局/命名空间范围内) - 感谢 JerryMyClass instance;int a[10] = {} (all zeroed) or int a[10] = {1,2}; (all zeroed except the first two items: a[0] == 1 and a[1] == 2)static (no matter if inside a function or in global/namespace scope) - thanks Jerry永远不要相信一个普通类型(int、long、...)的变量会被自动初始化!它可能会发生在像 C# 这样的语言中,但不会发生在 C &C++.
Never trust on a variable of a plain type (int, long, ...) being automatically initialized! It might happen in languages like C#, but not in C & C++.
这篇关于C++中的变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么两个函数的地址相同?Why do two functions have the same address?(为什么两个函数的地址相同?)
为什么 std::function 的初始化程序必须是可复制构Why the initializer of std::function has to be CopyConstructible?(为什么 std::function 的初始化程序必须是可复制构造的?)
混合模板与多态性mixing templates with polymorphism(混合模板与多态性)
我什么时候应该使用关键字“typename"?使用模When should I use the keyword quot;typenamequot; when using templates(我什么时候应该使用关键字“typename?使用模板时)
依赖名称解析命名空间 std/标准库Dependent name resolution amp; namespace std / Standard Library(依赖名称解析命名空间 std/标准库)
gcc 可以编译可变参数模板,而 clang 不能gcc can compile a variadic template while clang cannot(gcc 可以编译可变参数模板,而 clang 不能)