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

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

问题描述

在 Stroustrup 的C++ 编程语言:特别版(第 3 版)中,Stroustrup 写道,不仅允许,而且鼓励在控制语句的条件中声明和初始化变量.他写道,他鼓励这样做,因为它将变量的范围缩小到只有它们需要的范围.所以像这样......

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

...是很好的编程风格和实践.变量 i 仅存在于需要它的 if 语句块,然后超出范围.

...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.

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

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?

此外,出于好奇,人们是否普遍同意 Stroustrup 认为这是一种好的风格?或者这是一种语言的创造者在他的脑海中产生了一个不一定得到该语言社区支持的想法的情况?

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?

推荐答案

允许在嵌套块的控制部分声明变量,但在 if 的情况下而,变量必须初始化为将被解释为条件的数字或布尔值.它不能包含在更复杂的表达式中!

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.

我个人认为让局部变量尽可能接近它们在代码中的实际生命周期是一种很好的做法,即使当您从 C 切换到 C++ 或从 Pascal 切换到 C++ 时这听起来令人震惊——我们习惯于看到所有变量在一处.有了一些习惯,您会发现它更具可读性,并且您不必在其他地方查找声明.此外,您知道在此之前没有使用它.

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.

话虽如此,我认为在一个声明中混合过多并不是一个好习惯,我认为这是一个共同的观点.如果你影响了一个变量的值,然后在另一个表达式中使用它,那么通过将这两个部分分开,代码将更具可读性和更少的混乱.

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.

所以不要使用这个:

int i;
if((i = read(socket)) < 0) {
    // handle error
}
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;
}

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

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