English 中文(简体)
处理Bool Value with an If statement
原标题:Handling Bool Value With an If Statement

我是一位学生方案家,目前正在为Qt公司设计一个GUI,我有一个很简单的问题,即我似乎在网上找到答案。 如果你知道答案在何地,我很乐意提及,那么似乎像有人事先要求。 我的问题是:如果发言,可以采用C++中的“蓝色数据类型”。 因此,保ool价值要么是一或零,这样你就可以做这样的事情。

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

我在此真心想的是,这一ool干功能的最后一节。 当我使用回来时,我是否实际上将这一职能搁置起来,或者我是否不适当地使用关键词回来? 一经确认即为过去 我愿利用这一职能来表明是否在另一职能中继续工作。 请保持我的建设性批评。 作为一名学生,我只想改进。

最佳回答

您使用<代码>return说明返回的价值,在您称职时即刻予以退还。

因此,如果你在你方案的其他部分写成这一法典:

bool returnValue = trueOrFalse();

然后,<代码>returnValue变量将包含相当于<代码>trueOrFalse(功能>所交回的 Boo升值。

职能本身没有“固定”价值,职能召集人界定了一个在职能呼吁完成后具有价值的变化。


关于您的第二个问题,请使用<代码>return的声明加以更正,但可在trueOrFalse(功能中简化你的代码。 不这样做:

   if (validate == true)
   {
       return true;
   }
   else
   {
       return false;
   }

你们都需要:

return validate;

Because the validate local variable is already a bool!

This removes the redundancy of checking a Boolean value against a Boolean constant (true), which prevents strange err或s from creeping in and makes the code easier to read and understand.

事实上,在你重新研究波兰价值(<条码>bool)时,你可以采用这种一般模式。 不要把它们与字面<代码>false或true相比较,你只能写:

if (validate)  // test f或 truth
{
    // do whatever...
}

if (!validate)  // test f或 falsity
{
    // do whatever...
}
问题回答

替换

 if (validate == true)
       {
       return true;
       }
    else
       {
       return false;
       }

iii

return validate;

没有理由对其进行测试,并返回不同的羊毛。

在你回头时,取而代之的是以前。

if (validate == true)

iii

if (validate)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签