473,503 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

more effective c++ item 31

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.

Sep 26 '05 #1
4 2407
cf****@gmail.com 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
kb******@gascad.at
Sep 26 '05 #2
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!

Sep 27 '05 #3
cf****@gmail.com 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
kb******@gascad.at
Sep 27 '05 #4
thank you for your patience,
now i can keep reading on following items.

Sep 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17420
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
4
1965
by: John | last post by:
I am looking for a good Lock class implementation that has barely been implemented in Effective STL of Myers (Item 12). Any pointers? Is there an implementation that is good for both OpenMP and...
1
1611
by: FBergemann | last post by:
Hi, i have a little question concerning Scott Meyer's Effective C++ (2nd edition), Item 7 "Be prepared for out-of-memory conditions". It's about his example for a class specific new_handler()...
116
7414
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
44
3818
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
2
2059
by: Piotr | last post by:
I am reading Effective STL item 7 My understanding is, instead of doing this: tempate <typename T> struct DeleteObject: public unary_function<const T*, void> { void operator() (const T* ptr)...
3
1162
by: creator_bob | last post by:
I decided to post here after a little incident where I got an effective memory leak that was immune from garbage collection. I created a simple binary tree, no balancing or deleting. Only two...
7
3077
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
1
1915
by: mahesh.kanakaraj | last post by:
Hi All, I have a confusion in finding the 'effective content' of a complex type definition in a XML Schema. I shall give you an example situation to clearly explain my problem. Let's have...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.