局部变量范围问题 [英] Local variable scope question

查看:135
本文介绍了局部变量范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我认为根据C ++标准3.3.2

 <$ p 

c $ c> #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);
//将其改为如下将改变行为
// MyClass m(12345);
cout<< Y endl;

return 0;
}




基于响应,我可以假设 MyClass(12345); 是表达式(和范围)。这是有道理的。所以我期望下面的代码将总是打印xYx:

  MyClass(12345),cout< Y endl; 

并允许进行此类替换:

  //这个字符串有明确的范围
{
boost :: scoped_lock lock(my_mutex);
int x = some_func(); //应该在多线程程序中保护
}
//这里释放的互斥量

//

//我可以替换为以下一个字符串:
int x = boost :: scoped_lock(my_mutex),some_func(); //仍然是多线程安全的
//这里释放的互斥量


解决方案

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


已声明 > *在块中对于该块是本地的。


您没有声明任何 name 。您的

  MyClass(12345) 

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





c> call_a_function(MyClass(12345));

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


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?

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;
}


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);

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?

这篇关于局部变量范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆