Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multiple inheritance  (Read 1954 times)

0 Members and 1 Guest are viewing this topic.

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Multiple inheritance
« on: January 18, 2012, 06:17:08 pm »
I'm implementing my own collision sistem with collision shapes, and I'm using sf::ConvexShape, sf::CircleShape and own clases for this.

I use a vector to hold all shapes and then check for collision. So I use a base class type, CollisionShape.

Code: [Select]
std::vector<CollisionShape*> m_shapeList;

I have my own shape classes like Box, Circle, Polygon... and some of them inherit sf::ConvexShape and use its methods. I have something like this.



Note that there are more shapes inheriting CollisionShape class.

I have a problem when I do this:

Code: [Select]
Polygon* polygon = new Polygon();
addShape(polygon);
...
void addShape( CollisionShape* shape )
{
    shape->SetOutlineColor(...);     <----------------------
    m_shapeList.push_back(shape);
}


I can't use sf::ConvexShape 's methods, so I could change CollisionShape pointer to sf::ConvexShape pointer. But I have more shapes inheriting other classes (a.s.a sf::CircleShape).

Any OOP idea for solving this? A better design?

Hope someone can understand my problem,
Thanks!

Serapth

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Multiple inheritance
« Reply #1 on: January 18, 2012, 06:33:56 pm »
If you insist on going the inheritence route, why not use sf::Shape instead?

Additionally, are you sure it isn't a HasA relationship in that CollisionShape HasA Shape, not IsA shape, meaning you can implement the relationship as a member, preferably a templated one, if the signatures are consistent enough.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Multiple inheritance
« Reply #2 on: January 18, 2012, 07:58:59 pm »
If all your shape classes inherit from a sf::Shape class, you can cross-cast with dynamic_cast, but I don't know if cross-casting is good design-wise (never used it myself). I guess it's as bad as down-casting.

Transforming public inheritance to composition might be a good solution, if your public API doesn't depend too much on SFML shape's API.
Laurent Gomila - SFML developer

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Multiple inheritance
« Reply #3 on: January 18, 2012, 09:44:01 pm »
Yes, that's a nice solution. I wanted to know your opinions, so I'll try to implement the shape as a member for now. In fact, I wanted to avoid the use of multiple inheritance.

Thank you very much!

 

anything