What type of inheritance is preferable?

Is the only point in the implementation of the second case if I want to get from Collidable without being an object? If this is the case when it is the 1st case, it is ever favorable, since the second case offers great flexibility.

Both collidables have only a pure virtual function, and Object is the base class for objects that can be drawn on the screen (in my case).

^ Assuming I understand the following code correctly (I'm not sure about TBH)

class Object class CollidableObject : Object class Actor : public CollidableObject class Object class Collidable class Actor : public Object, public Collidable 

Edit:

Based on Matt / Seth

 class Object class Collidable class Clickable class Trackable class BlowUppable class Actor : public Object, public Collidable, public Clickable, public Trackable, public BlowUppable class SomeObjectThatIsTakenForGrantedThatEverythingElseIsInherited : public Actor 

The first example is the second case, and the second example is the first case. I assume this is the only use I see for the first case.

@Luchian
This will be a different question from the original, since your answer was neither one nor the other.

In this case, is there a difference that changes an object from an is-a to aa relationship? In each case, in order to check for collisions, the object must have a flag to know whether to check for a collision. In your case, the member can be checked if it is zero or not, but in the derived case the object itself reports whether it can collide or not. In an array / tree, I can either pass the derived object as an argument, or pass hitbox as an argument using the get () method.

To be deeper, I have another class - using the second case

 class Hitbox : public Object, public Collidable 

and the class Actor has it as a member

 class Actor : public Object { Hitbox *box; }; 

Objects that have a collision will have a hitbox instead, and this represents your message for sure, I think. But what else causes me is that when I look at your example again, does this mean that the Hitbox must have a Collidable member?

 class Hitbox { Collidable *collision; }; 

What I have:
The actor has a hitbox that handles the collision

What Hitbox should do:
Inherit collidable or
Affects as a member

The actor is already following your convention. Should Hitbox do the same?

+7
source share
3 answers

I would do the second case, since Object and Collidable are cross-cutting issues. If you go CollidableObject route, you are likely to come across a combinatorial explosion of classes. It won't be long until you see CollidableTrackableClickableBlowuppableObject .

Since Collidable is purely virtual, it is used in this case as an interface, so there are not many criticisms that contradict multiple inheritance. You simply declare that the Actor implements the Collidable interface.

+2
source

This is the perfect scenario for a strategy template . It is here that English plays tricks in our minds and makes us think that CollidableObject is a valid object for this hierarchy. I say that a collision is more a behavior than an object, so I would not go with any of them.

Prefer a composition for this:

 class Object { CollidableBehavior* collisionBehavior; } class Actor : Object { // collisionBehavior = new ActorCollision() } class AClassThatDoesntCollide { // collisionBehavior = NULL } 
+1
source

Is the only moment in the implementation of the second case, if I want to receive from a callable without an object?

Yes, the second case gives you more flexibility because it allows you to separate interfaces. For example, later you may want an object that can be fixed, but it is not available.

When the first case is ever favorable, since the second case offers more flexibility.

The second case offers more flexibility, but it is also more difficult to design. In the end, you will need virtual inheritance, and this is more difficult to handle. However, if your base classes are purely abstract, this should not be a problem.

You can see it .

0
source

All Articles