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(SpaceSh ip&){ ... }
void collide(SpaceSt ation&){ ... }
void collide(Asteroi d&){ ... }
};
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::coll ide(GameObject& o), and which, in turn, call
o.collide(*this ).
that's our newly added Satellite::coll ide(SpaceShip&) implementation.
and i don't think the existing SpaceShip needs to be added a virtual
member SpaceShip::coll ide(Satellite&) .
or i have misunderstandin g on this part?
thank you. 4 2420 cf****@gmail.co m wrote: hi,
how come? i assume:
class Satellite : public GameObject { void collide(GameObj &){ ... } void collide(SpaceSh ip&){ ... } void collide(SpaceSt ation&){ ... } void collide(Asteroi d&){ ... } };
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::coll ide(GameObject& o), and which, in turn, call o.collide(*this ). that's our newly added Satellite::coll ide(SpaceShip&) implementation. and i don't think the existing SpaceShip needs to be added a virtual member SpaceShip::coll ide(Satellite&) .
or i have misunderstandin g 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
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(SpaceSh ip&) = 0;
};
class SpaceShip : public GameObj
{
public:
virtual void collide(GameObj &);
virtual void collide(SpaceSh ip&);
};
void SpaceShip::coll ide(GameObj& other)
{
cout << "SpaceShip hits GameObj => ";
other.collide(* this);
}
void SpaceShip::coll ide(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(SpaceSh ip&);
};
void Satellite::coll ide(GameObj& other)
{
cout << "Satellite hits GameObj => ";
other.collide(* this);
}
void Satellite::coll ide(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(Satell ite&)"
, 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! cf****@gmail.co m 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(Satell ite&)" , 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
thank you for your patience,
now i can keep reading on following items. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. Yahoo store was originally written in Lisp.
c. Emacs
The issues with these will probably come up, so I might as well
mention them myself (which will also make this a more balanced
|
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 pthreads?
Thanks,
--j
|
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() management. Please
note that i don't want to start a discussion about the
purpose/usability of the approach. But i just want to understand some
technical issue of the example.
Here's the question:
Why does he define the member function
|
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 and some who couldn't but that it wasn't important right now.
And I said, 'sure, we can do that later'.
So now I've developed an app without any thought to security and am trying
to apply it afterwards. Doh!, doh! and triple doh!
|
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 specialized for
anything.
Thanks,
Josh McFarlane
| |
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) const
{
delete ptr;
|
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
functions from the root--update (add if missing), and read. The
problem was the update had a bug and after updating, it would always
then add as though the item were missing. This caused the tree to grow
unbounded, but from all outward...
|
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 item 7 for why this is virtual
...
|
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 an element declaration as follows:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |