Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualheh65532

Posted 16 May 2013 - 08:05 PM

hi,

i've been using this simple OO approach with rather simple code of determining if buildings have access between each other, in my game. But the problem is that it's quite clumsy way of doing OO and i think there's better solution in doing this in c++ but i can't remember what? The problem with this code is that there's possibility of infinite loop if i make some mistake in adding more classes that need this check.

 

so can you make a better OO solution for this check (code below)? 

 

 


class Bridge: public BaseBuilding
{
// return true if This building can be accessed from building b
bool canAccessFrom(BaseBuilding & b)
{
 if(b.canAccessFrom(*this)) // use the code in the other class if any...
return true;

return false;
}

};

class House: public BaseBuilding
{

bool canAccessFrom(BaseBuilding & b)
{
 return this->isOnSameLevel(b);
}

};
 

 

thanks. i hoipe i was able to explain what the problem is.


#1heh65532

Posted 16 May 2013 - 08:03 PM

hi,

i've been using this simple OO approach with rather simple code of determining if buildings have access between each other, in my game. But the problem is that it's quite clumsy way of doing OO and i think there's better solution in doing this in c++ but i can't remember what? The problem with this code is that there's possibility of infinite loop if i make some mistake in adding more classes that need this check.

 

so can you make a better OO solution for this check (code below)? 

 

 


class Bridge: public BaseBuilding
{
// return true if This building can be accessed from building b
bool canAccessFrom(BaseBuilding & b)
{
 if(b.canAccessFrom(*this))
return true;

return false;
}

};

class House: public BaseBuilding
{

bool canAccessFrom(BaseBuilding & b)
{
 return this->isOnSameLevel(b);
}

};
 

 

thanks. i hoipe i was able to explain what the problem is.


PARTNERS