C++ 类方法不返回值

C++ 类方法不返回值,c++,C++,我在一个类中有一个不返回值的方法。 这些是方法中使用的相关变量,它们位于类的私有部分 int hieght_of_plane = 0 ; int position_of_plane = 0 ; bool landing_gear = true ; bool is_flying = false ; bool is_alive = true ; 这是一个不返回true或false的方法,它位于类的公共部分 bool check_for_alive() { if (i

我在一个类中有一个不返回值的方法。 这些是方法中使用的相关变量,它们位于类的私有部分

int hieght_of_plane = 0 ;
int position_of_plane = 0 ;
bool landing_gear = true ;
bool is_flying = false ;
bool is_alive = true ;
这是一个不返回true或false的方法,它位于类的公共部分

    bool check_for_alive()
    {
        if (is_flying == false)
        {
            return true ;
        }
        if (is_flying == true)
        {
            if (hieght_of_plane <= 3)
            {
                if (landing_gear == false)
                {
                    is_alive = false ;
                    return false ;
                }
                else if (landing_gear == true)
                {
                    return true ;
                }
            }
        }
    }

我假设在这个例子中它应该返回false,因为is_flying=false,所以该方法应该返回false,但它不是。

失败的情况是当
flying==true
hight_of_plane
大于3时

    if (is_flying == true)
    {
        if (hieght_of_plane <= 3)
        {
您还可以将其简化为一个布尔表达式

编辑2:简化
这里有两个功能:返回条件组合的
true
/
false
,并根据条件设置
处于活动状态。
假设一个条件为真,所有其他条件为假

bool check_for_alive()
{
    if (is_flying && (height_of_plane <= 3) && (landing_gear == false))
    {
        is_alive = false;
    }
    return is_flying && (height_of_plane <= 3) && (landing_gear == true);
}
bool-check\u-for\u-alive()
{

如果(is_flying)&(height_of_plane控制流将在
is_flying为true
Hight_of_plane为>3
时到达功能的末尾。您必须在那里返回某些内容。 还有一件事是,你不必总是检查
if(something==true)
,相反
if(something)
就足够了,我已经在代码下面指出了更多

bool-check\u-for\u-alive(){
如果(!你在飞)
返回true;
//如果你到达这里,飞行是真的,不需要检查

if(hight_of_plane)当
if(hight_of_plane)时会产生警告,对于
is_flying==true&&hight_of_plane>3
的情况,您的方法会返回什么?此外,您不需要单独的“if”
is_flying
landing_gear
语句。
else
语句将处理此问题,例如,“if(is_flying)/*…*/else{/*…*/}
。else if“不是必需的,让它成为一个简单的
else`。当
is\u flying==true&&height\u of\u plane>3
时,它仍然给我相同的结果warning@peterthepiper68:完全正确。当
is_flying==true和&height_of_plane>3
为true时,您希望该方法返回什么值,为什么?
if (is_flying == false)
{
    return true;
} 
else // This means is_flying == true
{
    if (hieght_of_plane <= 3)
    {
        if (landing_gear == false)
        {
            is_alive = false ;
            return false ;
        }
        else // implies landing_gear == true
        {
            return true ;
        }
    else // implies hieght > 3
    {
         return ????
    }
}
bool check_for_alive()
{
    if (is_flying && (height_of_plane <= 3) && (landing_gear == false))
    {
        is_alive = false;
    }
    return is_flying && (height_of_plane <= 3) && (landing_gear == true);
}