Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > more effective c++ item 31

Reply
Thread Tools

more effective c++ item 31

 
 
cfchou@gmail.com
Guest
Posts: n/a
 
      09-26-2005
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.

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
 
 
 
cfchou@gmail.com
Guest
Posts: n/a
 
      09-27-2005
hi,
thank you for the reply.

but i'm afraid that my example didn't express my
question very well, sorry for that.

i write'n compile the following code(omited the
SpaceStation, Asteroid for clarity):

#include <iostream>
using namespace std;
class SpaceShip;
class GameObj
{
public:
virtual void collide(GameObj&) = 0;
virtual void collide(SpaceShip&) = 0;
};
class SpaceShip : public GameObj
{
public:
virtual void collide(GameObj&);
virtual void collide(SpaceShip&);
};
void SpaceShip::collide(GameObj& other)
{
cout << "SpaceShip hits GameObj => ";
other.collide(*this);
}
void SpaceShip::collide(SpaceShip& other)
{
cout << "SpaceShip hits SpaceShip" << endl;
}

//====here is the newlly added class====
class Satellite : public GameObj
{
public:
virtual void collide(GameObj&);
virtual void collide(SpaceShip&);
};
void Satellite::collide(GameObj& other)
{
cout << "Satellite hits GameObj => ";
other.collide(*this);
}
void Satellite::collide(SpaceShip& other)
{
cout << "Satellite hits SpaceShip" << endl;
}

void foo(GameObj& hitter, GameObj& hittee)
{
hitter.collide(hittee);
}

int main(int argc, char* argv[])
{
SpaceShip ss;
Satellite sl;
foo(ss, sl);
foo(sl, ss);
return 0;
}

the result:
SpaceShip hits GameObj => Satellite hits SpaceShip
Satellite hits GameObj => SpaceShip hits GameObj => Satellite hits
SpaceShip

my point is, althrough SpaceShip didn't know how to hit Satellite, but
eventually it can let Satellite do the job.
so, it seems that SpaceShip didn't need to be added a
"collide(Satellite&)"
, as long as any newlly added class knows how to hit the existing
classes.

i think i must ignore something on this topic, but i can't tell.
thanks for any help!

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-27-2005
wrote:
>
> the result:
> SpaceShip hits GameObj => Satellite hits SpaceShip
> Satellite hits GameObj => SpaceShip hits GameObj => Satellite hits
> SpaceShip
>
> my point is, althrough SpaceShip didn't know how to hit Satellite, but
> eventually it can let Satellite do the job.
> so, it seems that SpaceShip didn't need to be added a
> "collide(Satellite&)"
> , as long as any newlly added class knows how to hit the existing
> classes.
>
> i think i must ignore something on this topic, but i can't tell.


I see now.
You are assuming that a spaceship hitting a satellite is identical
to a satellite hitting a spaceship. Well. In this particular example
that assumption may be ok. But it is not in the general case:

A op B may not be identical to B op A

--
Karl Heinz Buchegger

 
Reply With Quote
 
cfchou@gmail.com
Guest
Posts: n/a
 
      09-27-2005
thank you for your patience,
now i can keep reading on following items.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Effective STL item 7 Piotr C++ 2 01-16-2006 11:03 PM
Effective C++ - item 7 (memory mgt). FBergemann@web.de C++ 1 08-07-2005 12:06 PM
meyers: Item 12: Effective C++ John C++ 4 04-27-2005 04:18 PM
Effective STL Item 4 (size() vs. empty()) Matthias C++ 25 02-01-2005 02:16 PM
Item 13 in Meyer's Effective C++ Don Kim C++ 9 05-23-2004 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57