Sign in to follow this  
Hop chewer

passing classes to function

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this