Inheritance is a strong, powerful weapon. Use it only when you really need it. Earlier, the inheritance of diamonds was a sign that I was going to go far according to classification, stating that the user is an “employee”, but they are also a “widget listener”, but also ...
In these cases, it is easy to hit several inheritance issues.
I solved them using composition and pointers back to the owner:
Before:
class Employee : public WidgetListener, public LectureAttendee { public: Employee(int x, int y) WidgetListener(x), LectureAttendee(y) {} };
After:
class Employee { public: Employee(int x, int y) : listener(this, x), attendee(this, y) {} WidgetListener listener; LectureAttendee attendee; };
Yes, access rights are different, but if you can avoid this approach without duplicating the code, this is better because it is less powerful. (You can save food if you have no alternative.)
anemptywhiteroom Sep 27 '08 at 6:09 2008-09-27 06:09
source share