C++ 使用 If 语句处理布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8844609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling Bool Value With an If Statement
提问by Wylie Coyote SG.
I am a student programmer currently designing a GUI for my company with Qt and I have a rather simple question that I just cant seem to find the answer to on the web. It seems like someone has had to of asked it before so if you know where the answer is Id be happy with a reference. My question is; can a Boolean datatype in c++ be handled using an if statement. So a bool value is either one or zero so can you do something like this
我是一名学生程序员,目前正在使用 Qt 为我的公司设计 GUI,我有一个相当简单的问题,我似乎无法在网络上找到答案。似乎有人之前不得不问过它,所以如果你知道答案在哪里,我会对参考感到满意。我的问题是;可以使用 if 语句处理 C++ 中的布尔数据类型。所以布尔值要么是一要么是零所以你可以做这样的事情
bool trueOrFalse()
{
myclass temp;
QString tempstr;
double candidate;
bool validate;
tempstr = ui->tableWidgetInjectionLocations->item(i,9)->text();
candidate = tempstr.toDouble(&validate);
if(validate == true)
{
temp.tempProperty = candidate;
}
else
{
QMessageBox error;
error.setText("Error");
error.exec();
}
if (validate == true)
{
return true;
}
else
{
return false;
}
}
What I am really looking for here is in the last section of this bool function. When I use return am I actually returning a value that this function would then hold or am I using a the keyword return inappropriately? Once validation has past Id like to be able to use the function to indicate whether or not to proceed in another function Please keep my criticism constructive please. As a student I am only interested in improving.
我真正要找的是这个 bool 函数的最后一部分。当我使用 return 时,我实际上返回了一个该函数将保留的值,还是我不恰当地使用了关键字 return?一旦验证通过 Id 希望能够使用该功能来指示是否继续在另一个功能中请保持我的批评建设性。作为一名学生,我只对改进感兴趣。
回答by Cody Gray
The value you return using the return
statement is literally returned whenever you call the function.
return
每当您调用该函数时,您使用该语句返回的值都会按字面意思返回。
So if you write this code somewhere else in your program:
因此,如果您在程序中的其他地方编写此代码:
bool returnValue = trueOrFalse();
then the returnValue
variable will contain a Boolean value equivalent to whatever was returned by the trueOrFalse()
function.
那么该returnValue
变量将包含一个与trueOrFalse()
函数返回的任何内容等效的布尔值。
The function itself doesn't "hold" the value, the caller of the function defines a variable that will hold the value after the function call has completed.
函数本身并不“保存”该值,函数的调用者定义了一个变量,该变量将在函数调用完成后保存该值。
As for your second question, you areusing the return
statement correctly, but you can simplify your code in the trueOrFalse()
function substantially. Instead of this:
至于你的第二个问题,你正在使用return
正确的语句,但可以简化在你的代码trueOrFalse()
基本功能。取而代之的是:
if (validate == true)
{
return true;
}
else
{
return false;
}
All you need is this:
你只需要这个:
return validate;
Because the validate
local variable is already a bool
!
因为validate
局部变量已经是一个bool
!
This removes the redundancy of checking a Boolean value against a Boolean constant (true
), which prevents strange errors from creeping in and makes the code easier to read and understand.
这消除了根据布尔常量 ( true
)检查布尔值的冗余,从而防止出现奇怪的错误并使代码更易于阅读和理解。
In fact, you can use this general pattern any time you're working with Boolean values (bool
). Rather than comparing them against the literal false
or true
, you can just write:
事实上,您可以在任何时候处理布尔值 ( bool
)时使用这种通用模式。而不是将它们与文字false
or进行比较true
,您可以只写:
if (validate) // test for truth
{
// do whatever...
}
or
或者
if (!validate) // test for falsity
{
// do whatever...
}
回答by Paul Tomblin
Replace
代替
if (validate == true)
{
return true;
}
else
{
return false;
}
with
和
return validate;
There is no reason to test it and return a different boolean.
没有理由测试它并返回不同的布尔值。
While you're at it, replace the previous
当你在做的时候,替换以前的
if (validate == true)
with
和
if (validate)