Sign in to follow this  
lucky6969b

How to improve this coding to favour composition over inheritance?

Recommended Posts

lucky6969b    1330

class Goods : public Objects

{

public:

    Goods() { }

    Goods(const Goods& goods) { this->mesh = goods.mesh; }

    Goods(SkinnedMesh *_mesh) : Objects(_mesh) { }

}



class Pallet : public Goods

{

    Pallet() { }

    Pallet(const Pallet& pallet) { this->mesh = pallet.mesh; }

    Pallet(SkinnedMesh *_mesh) : Goods(_mesh) { }

};



class Carton : public Goods

{

    Carton() { }

    Carton(const Carton& carton) { this->mesh = carton.mesh; }

    Carton(SkinnedMesh *_mesh) : Goods(_mesh) { }

}

 

You may consider Pallet and Carton may exhibit different behaviours.

Thanks

Jack

Share this post


Link to post
Share on other sites
Hodgman    51234
None of those classes do anything.
SkinnedMesh goods;
SkinnedMesh carton;
SkinnedMesh pallet;

You may consider Pallet and Carton may exhibit different behaviours.

It's kind of an incomplete question as is then. What is the purpose of the Pallet and Carton classes? What does the Objects class do? Why do Pallet and Carton inherit Goods to begin with? Edited by Hodgman

Share this post


Link to post
Share on other sites
Sandman    2210

It's kind of an incomplete question as is then. What is the purpose of the Pallet and Carton classes? What does the Objects class do? Why do Pallet and Carton inherit Goods to begin with?


Indeed, there isn't any way to 'favour composition over inheritance' because the inheritance doesn't actually do anything.

The only thing we can really suggest with this example is to stop using inheritance pointlessly and dump the Goods, Pallet and Cartons classes, because they don't do anything besides needlessly deepen your inheritance tree.

EDIT: missed the bit about Pallet and Carton exhibiting different behaviours. That does slightly change things, although it's still difficult to answer clearly without a bit more information about what those behaviour differences are. Edited by Sandman

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