Quote Originally Posted by marcvanriet View Post
Say you have a class 'bike', and if you have a class 'livingroom'. Both have a light that you can switch on and off. That doesn't mean it is a good idea to have a class 'light' and derive 'bike' and 'livingroom' from it.
What if you call the class 'Lightable' and not 'light'?
Qt Code:
  1. class Lightable {
  2. public:
  3. virtual void enableLight() { ... }
  4. virtual void disableLight() { ... }
  5. };
  6.  
  7. class Bike : public Vehice, public Lightable { ... };
  8. class LivingRoom : public Chamber, public Lightable { ... };
To copy to clipboard, switch view to plain text mode 
Sounds reasonable to me...


An alternative is to have an external helper class that implements solely the "shared" functionality and call that object's methods passing the object it is to manipulate. Then you need implementations of that class to handle objects of specific types (ones that have the "RGB" functionality and those of the "TIME" functionality). Patterns like 'visitor' or 'strategy' come to my mind here but probably more solutions are possible and viable.
In the end you have one "instance" of functionality per class instead of having one per item which escapes the memory hog trap.