• <legend id='cGMkk'><style id='cGMkk'><dir id='cGMkk'><q id='cGMkk'></q></dir></style></legend><tfoot id='cGMkk'></tfoot>

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

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

        局部变量作用域问题

        Local variable scope question(局部变量作用域问题)
      1. <i id='jlFPT'><tr id='jlFPT'><dt id='jlFPT'><q id='jlFPT'><span id='jlFPT'><b id='jlFPT'><form id='jlFPT'><ins id='jlFPT'></ins><ul id='jlFPT'></ul><sub id='jlFPT'></sub></form><legend id='jlFPT'></legend><bdo id='jlFPT'><pre id='jlFPT'><center id='jlFPT'></center></pre></bdo></b><th id='jlFPT'></th></span></q></dt></tr></i><div id='jlFPT'><tfoot id='jlFPT'></tfoot><dl id='jlFPT'><fieldset id='jlFPT'></fieldset></dl></div>

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

            <tfoot id='jlFPT'></tfoot>

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

                    <tbody id='jlFPT'></tbody>
                  <legend id='jlFPT'><style id='jlFPT'><dir id='jlFPT'><q id='jlFPT'></q></dir></style></legend>
                  本文介绍了局部变量作用域问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么下面的代码打印xxY"?局部变量不应该存在于整个函数的范围内吗?我可以使用这样的行为,还是会在未来的 C++ 标准中改变?

                  Why is the following code prints "xxY"? Shouldn't local variables live in the scope of whole function? Can I use such behavior or this will be changed in future C++ standard?

                  我认为根据 C++ 标准 3.3.2在块中声明的名称对该块来说是局部的.它的潜在范围从它的声明点开始,并在其声明区域的末尾结束."

                  I thought that according to C++ Standard 3.3.2 "A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends at the end of its declarative region."

                  #include <iostream>
                  using namespace std;
                  
                  class MyClass
                  {
                  public:
                    MyClass( int ) { cout << "x" << endl; };
                    ~MyClass() { cout << "x" << endl; };
                  };
                  
                  int main(int argc,char* argv[])
                  {
                    MyClass  (12345);
                  // changing it to the following will change the behavior
                  //MyClass m(12345);
                    cout << "Y" << endl;
                  
                    return 0;
                  }
                  

                  <小时>

                  根据响应,我可以假设 MyClass(12345); 是表达式(和范围).这是有道理的.所以我希望下面的代码总是打印xYx":


                  Based on the responses I can assume that MyClass(12345); is the expression (and scope). That is make sense. So I expect that the following code will print "xYx" always:

                  MyClass (12345), cout << "Y" << endl;
                  

                  并且允许进行这样的替换:

                  And it is allowed to make such replacement:

                  // this much strings with explicit scope
                  {
                    boost::scoped_lock lock(my_mutex);
                    int x = some_func(); // should be protected in multi-threaded program
                  } 
                  // mutex released here
                  
                  //    
                  
                  // I can replace with the following one string:
                  int x = boost::scoped_lock (my_mutex), some_func(); // still multi-thread safe
                  // mutex released here
                  

                  推荐答案

                  您正确引用了标准.让我强调一下:

                  You quoted standard correctly. Let me emphasize:

                  块中的名称 声明是该块的本地.它的潜在作用域从它的声明点开始,到其声明区域的末尾结束.

                  A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends at the end of its declarative region.

                  实际上,您没有声明任何名称.你的线

                  You didn't declare any name, actually. Your line

                  MyClass (12345);
                  

                  甚至不包含声明!它包含一个表达式,它创建一个 MyClass 的实例,计算表达式(但是,在这种特殊情况下没有什么可计算的),并将其结果转换为 void,并销毁在那里创建的对象.

                  does not even contain a declaration! What it contains is an expression that creates an instance of MyClass, computes the expression (however, in this particular case there's nothing to compute), and casts its result to void, and destroys the objects created there.

                  一个不那么令人困惑的事情听起来像

                  A less confusing thing would sound like

                  call_a_function(MyClass(12345));
                  

                  你看过很多次,知道它是如何工作的,不是吗?

                  You saw it many times and know how it works, don't you?

                  这篇关于局部变量作用域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 作为模板参数时出错)
                  <i id='092fZ'><tr id='092fZ'><dt id='092fZ'><q id='092fZ'><span id='092fZ'><b id='092fZ'><form id='092fZ'><ins id='092fZ'></ins><ul id='092fZ'></ul><sub id='092fZ'></sub></form><legend id='092fZ'></legend><bdo id='092fZ'><pre id='092fZ'><center id='092fZ'></center></pre></bdo></b><th id='092fZ'></th></span></q></dt></tr></i><div id='092fZ'><tfoot id='092fZ'></tfoot><dl id='092fZ'><fieldset id='092fZ'></fieldset></dl></div>
                    <tbody id='092fZ'></tbody>
                1. <legend id='092fZ'><style id='092fZ'><dir id='092fZ'><q id='092fZ'></q></dir></style></legend>
                    • <bdo id='092fZ'></bdo><ul id='092fZ'></ul>
                    • <small id='092fZ'></small><noframes id='092fZ'>

                    • <tfoot id='092fZ'></tfoot>