C
cfchou
hi,
i have one question about the item 31 of Meyers' More Effective c++.
in page 234, section "Using Virtual Functions Only", it says that
"for example, you decide to add a new class Satellite (inheriting
from GameObject) to your game, you'd have to add a new collide
function to each of the existing classes in the program".
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?
thank you.
i have one question about the item 31 of Meyers' More Effective c++.
in page 234, section "Using Virtual Functions Only", it says that
"for example, you decide to add a new class Satellite (inheriting
from GameObject) to your game, you'd have to add a new collide
function to each of the existing classes in the program".
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?
thank you.