在C ++中的Conditional或Control语句中声明和初始化变量 [英] Declaring and initializing a variable in a Conditional or Control statement in C++

查看:174
本文介绍了在C ++中的Conditional或Control语句中声明和初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Stroustrup的 中,Stroustrup写道,控制语句的条件语句中的变量的声明和初始化不仅被允许,而且被鼓励。他写道,他鼓励它,因为它将变量的范围减少到它们所需的范围。这样的东西...

  if((int i = read(socket))< 0){
//句柄错误
}
else if(i> 0){
//句柄输入
}
else {
return true;
}

...是良好的编程风格和实践。变量 i 只存在于如果语句的块,需要它,然后超出范围。 / p>

然而,编程语言的这个特性似乎不支持g ++(版本4.3.3 Ubuntu的特定编译),这是令人惊讶的。也许我只是调用g ++的标志,关闭它(我调用的标志是 -g -Wall )。我的版本的g ++在使用这些标志编译时返回以下编译错误:

  socket.cpp:130:error: expression''int'
socket.cpp:130:error:expected`)'before'int'


$ b b进一步的研究,我发现我似乎不是唯一的一个编译器不支持这一点。在此问题中似乎有些混乱。 a>到底是什么语法被认为是标准的语言和什么编译器与它编译。



所以问题是,什么编译器支持这个功能,什么标志需要设置为它编译?



另外,出于好奇,人们是否普遍同意Stroustrup这样的好习惯?

解决方案

p>允许在嵌套块的控制部分中声明一个变量,但是如果,变量必须初始化为将被解释为条件的数值或布尔值。 不能包含在更复杂的表达式中!



在您显示的特定情况下,似乎没有办法



我个人认为,在本地变量尽可能接近代码中的实际生命周期时,这是一个很好的做法,即使在切换时C到C ++或从Pascal到C ++ - 我们习惯于在一个地方看到所有的变量。有了一些习惯,你会发现它更可读,你不必去别处找到声明。



编辑:

/ p>

话虽如此,我不觉得在单个语句中混合太多是一个好习惯,我认为这是一个共同的观点。如果你将一个值影响到一个变量,然后在另一个表达式中使用它,代码将更加可读,更少混淆通过分离这两个部分。



所以, :

  int i; 
if((i = read(socket))<0){
//句柄错误
}
else if(i> 0){
/ / handle input
}
else {
return true;
}

我更喜欢:

  int i = read(socket); 
if(i< 0){
// handle error
}
else if(i> 0){
// handle input
}
else {
return true;
}


In Stroustrup's The C++ Programming Language: Special Edition (3rd Ed), Stroustrup writes that the declaration and initialization of variables in the conditionals of control statements is not only allowed, but encouraged. He writes that he encourages it because it reduces the scope of the variables to only the scope that they are required for. So something like this...

if ((int i = read(socket)) < 0) {
    // handle error
}
else if (i > 0) {
    // handle input
}
else {
    return true;
}

...is good programming style and practice. The variable i only exists for the block of if statements for which it is needed and then goes out of scope.

However, this feature of the programming language doesn't seem to be supported by g++ (version 4.3.3 Ubuntu specific compile), which is surprising to me. Perhaps I'm just calling g++ with a flag that turns it off (the flags I've called are -g and -Wall). My version of g++ returns the following compile error when compiling with those flags:

socket.cpp:130: error: expected primary-expression before ‘int’
socket.cpp:130: error: expected `)' before ‘int’

On further research I discovered that I didn't seem to be the only one with a compiler that doesn't support this. And there seemed to be some confusion in this question as to exactly what syntax was supposedly standard in the language and what compilers compile with it.

So the question is, what compilers support this feature and what flags need to be set for it to compile? Is it an issue of being in certain standards and not in others?

Also, just out of curiosity, do people generally agree with Stroustrup that this is good style? Or is this a situation where the creator of a language gets an idea in his head which is not necessarily supported by the language's community?

解决方案

It is allowed to declare a variable in the control part of a nested block, but in the case of if and while, the variable must be initialized to a numeric or boolean value that will be interpreted as the condition. It cannot be included in a more complex expression!

In the particular case you show, it doesn't seem you can find a way to comply unfortunately.

I personally think it's good practice to keep the local variables as close as possible to their actual lifetime in the code, even if that sounds shocking when you switch from C to C++ or from Pascal to C++ - we were used to see all the variables at one place. With some habit, you find it more readable, and you don't have to look elsewhere to find the declaration. Moreover, you know that it is not used before that point.


Edit:

That being said, I don't find it a good practice to mix too much in a single statement, and I think it's a shared opinion. If you affect a value to a variable, then use it in another expression, the code will be more readable and less confusing by separating both parts.

So rather than using this:

int i;
if((i = read(socket)) < 0) {
    // handle error
}
else if(i > 0) {
    // handle input
}
else {
    return true;
}

I would prefer that:

int i = read(socket);
if(i < 0) {
    // handle error
}
else if(i > 0) {
    // handle input
}
else {
    return true;
}

这篇关于在C ++中的Conditional或Control语句中声明和初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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