Sign in to follow this  
Decrius

Code structure: inheritance and composition

Recommended Posts

I wrote code using inheritance, to avoid code-reuse. I just noticed this is not the purpose of inheritance...so I am wondering, WHAT IS the purpose of inheritance then? Also, how would I correctly structure my code, what classes and class connections should be made? I am writing my own little GUI system, where I have the following classes and inheritance (which is wrong I guess! this is just what I have now): - References (a reference count class, memory handling) - Signals derives from References (signal and slot system for events) - Container derives from Signals - Custom containers derive from container - Component derives from Signals - Custom component derive from component Each class needs References, to keep memory handled correctly. Then, both container and component need signals (prefered derived from! so one can use functions of signal as if it is a container/component function). The custom classes derive from their base classes and give functionality. Container and component need a virtual draw function which should be overwritten by the custom classes, however this is not possible (get a compiler error). Of a solution I thought of having instance of classes in the class instead to derive from them, would work for the reference class at least, not sure if it's a good idea for the signal class... How would one organise this code, where to use inheritance and where to use composition? Thanks!

Share this post


Link to post
Share on other sites
Use inheritance to implement polymorphism, and composition everywhere else. (readme). Polymorphism is a way of reusing code: you don't use the code of the base class (that is what composition is for) but instead use the code that operates on the base class.

So, if you want to use a Dog, composition (a Family has-a Dog), but if you want to use a Leash, inherit from Dog (a Cocker Spaniel is-a Dog).

Aside from the custom component inheriting from a component, I see no valid inheritance in your message.

Share this post


Link to post
Share on other sites
Inheritance's purpose isn't code reuse, it's to implement is-a relationships. I am unclear on what "Signals" are. If it's just an object that can handle events(in which case a name like "SignaledObject" or something would be more descriptive), then I don't see anything particularly wrong with your inheritance design. Component is-a SignaledObject. That makes sense.

Share this post


Link to post
Share on other sites
Yes, mikeman.

The problem though, is that all classes are ReferenceCounters...so in fact all classes should derive from References using the is-a model. Where containers and components are SignalObjects AND ReferenceCounters. I could let them both derive from 2 base classes...the SignalObject would in that case never need to be derived from ReferenceCounter, as SignalObject would never be used as a stand-alone class.

In fact, I extend the container/component class with signal-slot functions. A container/component is-a SignalObject and is-a ReferenceCounter.

Will read your article, ToohrVyk.

Share this post


Link to post
Share on other sites
Okay, I think I get it.

Component HAS-A ReferenceCounterObject as well as a SignalEmittingObject, where as a custom component IS-A component. I now simply initiate the ReferenceCounterObject and SignalEmittingObject as objects in the class, instead to derive from them. I think this allows better flexibility of the code (like I read in ToohrVyk's post).

Also, with inheritance I would (actually) require Component/Container to derive from ReferenceCounterObject AND SignalEmittingObject...and this creates some difficulties with casting...so it's better with out :)

And, this way I use inheritance only where it is really requires (for custom variants of Component/Container!).

So, thanks guys! :)

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