Sign in to follow this  

Function calling with inherited classes and multiple parameters

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ugh, the title sucks but I'm not sure how to phrase it.. So suppose I have a base class Shape, and inherited from it I have Square, Circle and Triangle. I have a ShapeManager class that performs some operation on pairs of shapes. It stores the shapes as a list of Shapes. So if I have some function Update(Shape s1, Shape s2), what do I do if I don't know which shape is which, and different methods are used depending on the pairing of shapes. i.e. i'd like to do Update(Circle c, Square s); Update(Square s, Triangle t); Update(Triangle t, Circle c); without also having to do Update(Square s, Circle c); Update(Triangle t, Square s); Update(Circle c, Triangle t); but I don't have to worry about getting the parameters in the right order, since it'd introduce reasonable overheads in trying to sort them out. is there some way to have different function prototypes call the same body?

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
i.e. i'd like to do

Update(Circle c, Square s);
Update(Square s, Triangle t);
Update(Triangle t, Circle c);

without also having to do

Update(Square s, Circle c);
Update(Triangle t, Square s);
Update(Circle c, Triangle t);


Just go the same route you'd use with operator overloading:

Update(Circle c, Square s)
{
// do all your logic
}

Update(Square s, Circle c)
{
Update(c, s);
}

The second one will call the first one that has all the logic in it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegumsis there some way to have different function prototypes call the same body?


The short answer is 'no' - the best you can do is have one of the functions calling the other (and switching the parameters). Or introduce a third 'utility' function which does the actual work and is called by the other two.

But is it not possible to define an Update functions which just takes two Shapes? Provide the functionality you need for Update in the Shape base class and then you can avoid needing multiple functions at all?

Share this post


Link to post
Share on other sites
I tried to use shapes to keep the explanation clear, but it seems i may have made it slightly worse.

in truth the objects are spheres, cubes and planes. I'm trying to write a function for the object manager that can perform collision checks with every object. So PolyVox, I don't think I can just pass two shapes, since each shape has individual properties that effect how the collision check is done.

Share this post


Link to post
Share on other sites
ok hit a weird snag.

the manager stores everything as a Shape, and iterates through the lists and passes them to the Update() functions as Shapes.

As a result it starts whining, since there aren't any Update() functions that take Shapes, just Triangles, Circles and Squares.

Share this post


Link to post
Share on other sites
still not sure how to proceed with this inherited class problem, anyone able to help?

if i store all the Shapes as Shapes in the ShapeManager, and I want to call the Update() function on pairs of shapes, how do I make it realise that some Shape is also a Triangle or a Circle? I could maintain seperate lists for each type, or create an enumerator, but I thought there might be a slicker way of doing it?

Share this post


Link to post
Share on other sites

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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