<bdo id='0Zy0p'></bdo><ul id='0Zy0p'></ul>

    <i id='0Zy0p'><tr id='0Zy0p'><dt id='0Zy0p'><q id='0Zy0p'><span id='0Zy0p'><b id='0Zy0p'><form id='0Zy0p'><ins id='0Zy0p'></ins><ul id='0Zy0p'></ul><sub id='0Zy0p'></sub></form><legend id='0Zy0p'></legend><bdo id='0Zy0p'><pre id='0Zy0p'><center id='0Zy0p'></center></pre></bdo></b><th id='0Zy0p'></th></span></q></dt></tr></i><div id='0Zy0p'><tfoot id='0Zy0p'></tfoot><dl id='0Zy0p'><fieldset id='0Zy0p'></fieldset></dl></div>
    <tfoot id='0Zy0p'></tfoot>

  1. <small id='0Zy0p'></small><noframes id='0Zy0p'>

    <legend id='0Zy0p'><style id='0Zy0p'><dir id='0Zy0p'><q id='0Zy0p'></q></dir></style></legend>

      C++中的变量初始化

      时间:2023-05-24

      <small id='E2FJd'></small><noframes id='E2FJd'>

          <tbody id='E2FJd'></tbody>

        <i id='E2FJd'><tr id='E2FJd'><dt id='E2FJd'><q id='E2FJd'><span id='E2FJd'><b id='E2FJd'><form id='E2FJd'><ins id='E2FJd'></ins><ul id='E2FJd'></ul><sub id='E2FJd'></sub></form><legend id='E2FJd'></legend><bdo id='E2FJd'><pre id='E2FJd'><center id='E2FJd'></center></pre></bdo></b><th id='E2FJd'></th></span></q></dt></tr></i><div id='E2FJd'><tfoot id='E2FJd'></tfoot><dl id='E2FJd'><fieldset id='E2FJd'></fieldset></dl></div>
        • <legend id='E2FJd'><style id='E2FJd'><dir id='E2FJd'><q id='E2FJd'></q></dir></style></legend>

        • <tfoot id='E2FJd'></tfoot>
            <bdo id='E2FJd'></bdo><ul id='E2FJd'></ul>
              1. 本文介绍了C++中的变量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我的理解是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] == 1a[1] == 2)
                  • 同样适用于非聚合类/结构,例如MyClass 实例 = {};(关于这方面的更多信息可以在 这里)
                  • 这是一个全局/外部变量
                  • 变量被定义为static(无论是在函数内还是在全局/命名空间范围内) - 感谢 Jerry
                  • it's a class/struct instance in which the default constructor initializes all primitive types; like MyClass instance;
                  • you use array initializer syntax, e.g. 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)
                  • same applies to non-aggregate classes/structs, e.g. MyClass instance = {}; (more information on this can be found here)
                  • it's a global/extern variable
                  • the variable is defined 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模板网!

                上一篇:在 static_assert 输出中集成类型名称? 下一篇:变量上的 const 与 constexpr

                相关文章

                最新文章

                    <bdo id='vpt1q'></bdo><ul id='vpt1q'></ul>
                  <legend id='vpt1q'><style id='vpt1q'><dir id='vpt1q'><q id='vpt1q'></q></dir></style></legend>

                1. <i id='vpt1q'><tr id='vpt1q'><dt id='vpt1q'><q id='vpt1q'><span id='vpt1q'><b id='vpt1q'><form id='vpt1q'><ins id='vpt1q'></ins><ul id='vpt1q'></ul><sub id='vpt1q'></sub></form><legend id='vpt1q'></legend><bdo id='vpt1q'><pre id='vpt1q'><center id='vpt1q'></center></pre></bdo></b><th id='vpt1q'></th></span></q></dt></tr></i><div id='vpt1q'><tfoot id='vpt1q'></tfoot><dl id='vpt1q'><fieldset id='vpt1q'></fieldset></dl></div>

                  <small id='vpt1q'></small><noframes id='vpt1q'>

                2. <tfoot id='vpt1q'></tfoot>