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

    2. <small id='80BcM'></small><noframes id='80BcM'>

        <bdo id='80BcM'></bdo><ul id='80BcM'></ul>

      C++ 全局变量声明

      时间:2023-05-24
      <tfoot id='9sRwa'></tfoot>

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

            <small id='9sRwa'></small><noframes id='9sRwa'>

              <tbody id='9sRwa'></tbody>

              1. <legend id='9sRwa'><style id='9sRwa'><dir id='9sRwa'><q id='9sRwa'></q></dir></style></legend>
                本文介绍了C++ 全局变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..


                我想要做的只是在头文件中定义一个变量,并在两个不同的 cpp 文件中使用它,而无需在每次包含该头文件时重新定义该变量
                这是我尝试的方法:

                变量.h


                What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header
                Here is how I tried :

                Variables.h

                #ifndef VARIABLES_H // header guards
                #define VARIABLES_H
                
                static bool bShouldRegister;
                
                #endif
                

                (我也试过 extern 但没有任何改变)

                (I also tried extern but nothing changed)

                在一个 cpp 文件中,我给它一个值 ::bShouldRegister = truebShouldRegister = true;

                And in a cpp file I give it a value ::bShouldRegister = true or bShouldRegister = true;

                在我的另一个 cpp 文件中,我通过创建一个线程并在循环中检查它的值来检查它的值(是的,我的线程功能运行良好)

                In my another cpp file I check it's value by creating a thread and check its value in a loop (and yes my thread function works well)

                 while (true)
                 {
                     if (::bShouldRegister) // Or if (bShouldRegister)
                        {
                            MessageBox(NULL,"Value Changed","Done",MB_OK|MB_ICONINFORMATION);
                        }
                  Sleep(100);
                 }
                

                是的,MessageBox 永远不会出现(bShouldRegister 永远不会为真:/)

                And yes, that MessageBox never appears (bShouldRegister never gets true :/)

                推荐答案

                必须使用 extern,否则每个翻译单元中的 bShouldRegister 变量可能不同值.

                You must use extern, otherwise you will have separated bShouldRegister variables in each translation unit with probably different values.

                把它放在一个头文件(.h)中:

                Put this in a header file (.h):

                extern bool bShouldRegister;
                

                把它放在一个实现文件 (.cpp) 中:

                Put this in one of implementation files (.cpp):

                bool bShouldRegister;
                

                这篇关于C++ 全局变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:是否有可能使函数接受给定参数的多种数据类型 下一篇:如何在 windows (win32) 上获取每个线程的 CPU 使用率

                相关文章

                最新文章

                <tfoot id='2yEU4'></tfoot>
                  <bdo id='2yEU4'></bdo><ul id='2yEU4'></ul>
              2. <legend id='2yEU4'><style id='2yEU4'><dir id='2yEU4'><q id='2yEU4'></q></dir></style></legend>

                <small id='2yEU4'></small><noframes id='2yEU4'>

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