Mix and match class in C++/MFC
- by Coder
I'm trying to re-factor a code base, and there is some common functionality among unrelated classes that I'd love to unite. I would like to add that functionality in common base class, but I'm not sure if it's clean and good approach.
Say I have CMyWnd class and CMyDialogEx class, both different, so they cannot inherit from one base class. I want to add a button to both classes and add the message handlers to both classes as well.
So I'd like to do something like this:
CMyWnd : public CWnd, public COnOkBtnFunctionality, public COnCancelBtnFunctionality
CMyDialogEx: public CWnd, public COnOkBtnFunctionality
Where COnOkBtnFunctionality would define CButton m_buttonOk, and all the afx_msg functions it should have. And so on.
Is this approach doable/good? Or are there better patterns I should resort to?