• <small id='5khNG'></small><noframes id='5khNG'>

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

      1. 全局作用域与全局命名空间

        Global scope vs global namespace(全局作用域与全局命名空间)
          <bdo id='UKFDv'></bdo><ul id='UKFDv'></ul>

            • <tfoot id='UKFDv'></tfoot>

              • <legend id='UKFDv'><style id='UKFDv'><dir id='UKFDv'><q id='UKFDv'></q></dir></style></legend>
                  <tbody id='UKFDv'></tbody>

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

              • <small id='UKFDv'></small><noframes id='UKFDv'>

                  本文介绍了全局作用域与全局命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看到了这两个短语的用法:全局作用域和全局命名空间.它们有什么区别?

                  I saw usages of these two phrases: global scope and global namespace. What is the difference between them?

                  推荐答案

                  在 C++ 中,每个名称都有其不存在的范围.作用域可以通过多种方式定义:它可以由命名空间函数和仅{}.

                  In C++, every name has its scope outside which it doesn't exist. A scope can be defined by many ways : it can be defined by namespace, functions, classes and just { }.

                  所以一个命名空间,无论是全局的还是其他的,定义了一个范围.全局命名空间是指使用::,在这个命名空间中定义的符号被称为具有全局作用域.默认情况下,符号存在于全局命名空间中,除非它定义在以关键字 namespace 开头的块中,或者它是类的成员,或者函数的局部变量:

                  So a namespace, global or otherwise, defines a scope. The global namespace refers to using ::, and the symbols defined in this namespace are said to have global scope. A symbol, by default, exists in a global namespace, unless it is defined inside a block starts with keyword namespace, or it is a member of a class, or a local variable of a function:

                  int a; //this a is defined in global namespace
                         //which means, its scope is global. It exists everywhere.
                  
                  namespace N
                  {
                       int a;  //it is defined in a non-global namespace called `N`
                               //outside N it doesn't exist.
                  }
                  void f()
                  {
                     int a;  //its scope is the function itself.
                             //outside the function, a doesn't exist.
                     {
                          int a; //the curly braces defines this a's scope!
                     }
                  }
                  class A
                  {
                     int a;  //its scope is the class itself.
                             //outside A, it doesn't exist.
                  };
                  

                  另请注意,name 可以被命名空间、函数或类定义的内部作用域隐藏.因此,名称空间 N 中的名称 a 在全局命名空间中隐藏了名称 a.同样,函数和类中的名称隐藏了全局命名空间中的名称.如果你遇到这种情况,那么你可以使用::a来引用全局命名空间中定义的名字:

                  Also note that a name can be hidden by inner scope defined by either namespace, function, or class. So the name a inside namespace N hides the name a in the global namspace. In the same way, the name in the function and class hides the name in the global namespace. If you face such situation, then you can use ::a to refer to the name defined in the global namespace:

                  int a = 10;
                  
                  namespace N
                  {
                      int a = 100;
                  
                      void f()
                      {
                           int a = 1000;
                           std::cout << a << std::endl;      //prints 1000
                           std::cout << N::a << std::endl;   //prints 100 
                           std::cout << ::a << std::endl;    //prints 10
                      }
                  }
                  

                  这篇关于全局作用域与全局命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                    <tbody id='Zu2ZA'></tbody>
                  <i id='Zu2ZA'><tr id='Zu2ZA'><dt id='Zu2ZA'><q id='Zu2ZA'><span id='Zu2ZA'><b id='Zu2ZA'><form id='Zu2ZA'><ins id='Zu2ZA'></ins><ul id='Zu2ZA'></ul><sub id='Zu2ZA'></sub></form><legend id='Zu2ZA'></legend><bdo id='Zu2ZA'><pre id='Zu2ZA'><center id='Zu2ZA'></center></pre></bdo></b><th id='Zu2ZA'></th></span></q></dt></tr></i><div id='Zu2ZA'><tfoot id='Zu2ZA'></tfoot><dl id='Zu2ZA'><fieldset id='Zu2ZA'></fieldset></dl></div>
                    <legend id='Zu2ZA'><style id='Zu2ZA'><dir id='Zu2ZA'><q id='Zu2ZA'></q></dir></style></legend>

                  1. <tfoot id='Zu2ZA'></tfoot>

                        • <bdo id='Zu2ZA'></bdo><ul id='Zu2ZA'></ul>

                        • <small id='Zu2ZA'></small><noframes id='Zu2ZA'>