Public Group

# Function calling with inherited classes and multiple parameters

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

## 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 on other sites
Quote:
 Original post by Winegumsi.e. i'd like to doUpdate(Circle c, Square s);Update(Square s, Triangle t);Update(Triangle t, Circle c);without also having to doUpdate(Square s, Circle c);Update(Triangle t, Square s);Update(Circle c, Triangle t);

Update(Circle c, Square s)
{
}

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

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

##### 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 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 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 on other sites
If the functions in question were implemented as single argument virtual member functions, you could use double dispatch.

##### 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?

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013257
×