我尋找到實現該場景的最佳方式:最好的方式,如果其它目的之一是真還是假
我有4個對象具有在應用的流量有時它們被設置布爾成員根據情況設定爲真,有時設定爲假;
然後,我有最終的功能,得到1這個對象,並需要檢查,如果在其他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;
}
有沒有做到這一點檢查更短的和更清潔的方式功能?
如果你會很好提到了這是哪種編程語言。 – JJJ 2015-01-04 12:24:12
我做了一些C++,但它真的沒關係 – user63898 2015-01-04 12:27:51