wrote:
>
> hi,
>
> how come? i assume:
>
> class Satellite : public GameObject
> {
> void collide(GameObj&){ ... }
> void collide(SpaceShip&){ ... }
> void collide(SpaceStation&){ ... }
> void collide(Asteroid&){ ... }
> };
>
> then the client code:
>
> void foo(Satellite& o)
> {
> SpaceShip s;
> s.collide(o); // o.collide(*this)
> }
>
> SpaceShip has no idea of Satellite, but s still can invoke
> SpaceShip::collide(GameObject& o), and which, in turn, call
> o.collide(*this).
> that's our newly added Satellite::collide(SpaceShip&) implementation.
> and i don't think the existing SpaceShip needs to be added a virtual
> member SpaceShip::collide(Satellite&).
>
> or i have misunderstanding on this part?
You assume, that you know the type of object.
Look at the follwing:
std::vector< GameObject* > AllObjects; // Pointers to all objects in the secenery
// be it SpaceShips, Sattellites, Planets, etc.
void foo( GameObject* OtherObject )
{
for( size_t i = 0; i < AllObjects.size(); ++i )
if( AllObjects[i] != OtherObject )
AllObjects[i]->collide( OtherObject );
}
A perfectly reasonable function, that checks if some object (no matter what) collides
with some object object in your scenery.
At the moment, you know the exact object type, you don't need a virtual function at all.
Virtual functions enter the picture only in the case that you don't have that information.
--
Karl Heinz Buchegger