passing classes to function

Started by
1 comment, last by DaveMS 14 years, 11 months ago
Hello, I have three classes that I need to pass to a function to check for collisions. I need to check for collisions between a ball and either paddle1 or paddle2. I want to have only two arguments in my function that will take class ball and either class paddle1 or class paddle2. I am not sure how to set it up so that argument two will take either paddle1 or paddle2. I have the following so far: bool collisionP1(Ball &ball, Paddle1 &paddle1) Rather than having two functions(one for paddle1 and one for paddle2) I would like to have one function and be able to pass either paddle1 or paddle2. How would I do this? Thanks
Advertisement
You shouldn't need two separate classes for each Paddle. You can just write one class, and create two instances. Can we see your Paddle classes?
As rip-off says, you shouldn't need a class for each paddle.

How ever, if your staying with that design, you could overload the function.
You'll still need to write two functions however.

bool Collision(Ball &ball, Paddle1& paddle);bool Collision(Ball &ball, Paddle2& paddle);

You could also derive them both from a base class.
class Paddle{};class Paddle1: public Paddle{};class Paddle2 : public Paddle{};bool Collision(Ball & ball, Paddle* paddle);


But again, the best solution is to stick to one paddle class.

It's unlikely there is enough difference in behaviour of each paddle to warrant seperate classes. What is it exactly that one paddle does that the other doesn't?

This topic is closed to new replies.

Advertisement