|
-
May 10th, 2010, 12:19 PM
#1
Function overloading
Hi to everyone,
I have a little problem with my classes but before describing the problem I wish to ask forgiveness if similar questions were made before. This forum is huge and I'm unsure what to search so my apologize if made another useless post.
The problem is this one:
I have an abstract class named Collision wich have a pure virtual method "collide" which get a Collision* in input. I also have two implementations: CollisionBox and CollisionSphere. Both of these class implement the "collide" method once for each kind of collision (Box to Box, Sphere to Sphere, Box to Sphere).
Now the problem is: if I implement the "collide" method like these way (once in CollisionBox and then again in CollisionSphere):
Code:
bool CollideBox::collide(Collision* collident) {...}
bool CollideBox::collide(CollisionBox* collident) {...}
bool CollideBox::collide(CollisionSphere* collident) {...}
I usually have a Collision* pointer to pass to these method so always the first method will be called unless I dynamic cast the pointer to the right class... for example if I do:
Code:
CollisionBox* box = new CollisionBox();
Collision* generic = new CollisionBox();
box->collide(generic);
I will get the first method to be called but I wish the second one (collide(CollisionBox*) ) to be called.
Now actually I had to put a brutal switch and many dynamic casts in the general method (collide(Collision*)) which cast the pointer and call again the (hopefully) right "collide" method.
Is there any other non-brutal way or pattern to create these cross calls between the implementations of the classes? I'm going to add more classes and I cannot make so many method whenever I add, move or edit a class.
Thank you for the answers. I hope I was clear enough to explain my problem, if not then tell me: I'll try to make a better explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|