C++ 检查其他对象之一是否为真的最佳方法

C++ 检查其他对象之一是否为真的最佳方法,c++,function,C++,Function,我正在寻找实现此方案的最佳方法: 我有4个对象,它们有布尔成员,在应用程序流中,它们有时被设置为true,有时被设置为false,具体取决于条件 然后我有最后一个函数,它得到了这个对象中的一个,需要检查其他3个对象中是否有一个将成员设置为true 问题是我知道如何进行脏检查,我正在寻找更干净的方法这是我的最终函数代码: class Obj { public : Obj(int _id) : id(_id) bool status; int id // only 4 object

我正在寻找实现此方案的最佳方法:

我有4个对象,它们有布尔成员,在应用程序流中,它们有时被设置为true,有时被设置为false,具体取决于条件

然后我有最后一个函数,它得到了这个对象中的一个,需要检查其他3个对象中是否有一个将成员设置为true

问题是我知道如何进行脏检查,我正在寻找更干净的方法这是我的最终函数代码:

class Obj
{
public :
   Obj(int _id) : id(_id)
   bool status;
   int id // only 4 objects are created 0,1,2,3
}

m_obj0 = new Obj(0) ;
m_obj1 = new Obj(1) ;
m_obj2 = new Obj(2) ;
m_obj3 = new Obj(3) ;

bool check(Obj* obj)
{
   if(obj->id == 0)
   {
      if(m_obj1->status || m_obj2->status || m_obj3->status)
      {
          return true;
      }
      return false;
   }else if(obj->id == 1)(
      if(m_obj0->status || m_obj2->status || m_obj3->status)
      {
          return true;
      }
      return false;
   }else if(obj->id == 2)(
      if(m_obj0->status || m_obj1->status || m_obj3->status)
      {
          return true;
      }
      return false;
   }else if(obj->id == 3)(
      if(m_obj0->status || m_obj1->status || m_obj2->status)
      {
          return true;
      }
      return false;

}

是否有一种更短更干净的方法来完成此检查功能

您可以将m_obj设置为数组。然后使用for循环进行检查

bool check(Obj* obj)
{
    for (int i = 0; i < 4; i ++) {
        if (obj->id == i) continue;
        if (m_obj[i]->status == true)
            return true;
    }
    return false;
}

如果你提到了这是什么编程语言,那就太好了。我在C++中做过,但是它确实不重要,它也可以选择,顺便说一下,我只需要其中一个是真的,这样你就可以添加中断了。在第一次发现之后。但我不知道是否有更好的方法。。谢谢@user63898函数在第一次查找后将返回true,返回后您不需要中断。如果我知道我只有4个对象要检查它们,就像我执行循环检查一样,那么什么更有效?@user63898您喜欢新的一个吗?
bool check(Obj* obj)
{
    int result = m_obj[0]->status+m_obj[1]->statusm_obj[2]->status
                +m_obj[3]->status-m_obj[obj->id]->status;
    return (result!=0);
}