Possible? Injection of a CWndEx-Class in MFC-structure?
-
Hello, I would like to add a new member-function to all the controls of different kind in my dialog, e.g. CEdit, CButton and so on. Since they are all derived from CWnd I would like to know if there is a possibility to insert a new class "CWndEx" between CWnd and the control class, so that e.g. CEdit is not longer derived directly from CWnd but from CWndEx. This way I could add a new member function to CWndEx so all the my controls would inherit this function? Seems to be a newbie :confused: question, yes? Thank you very much...
-
Hello, I would like to add a new member-function to all the controls of different kind in my dialog, e.g. CEdit, CButton and so on. Since they are all derived from CWnd I would like to know if there is a possibility to insert a new class "CWndEx" between CWnd and the control class, so that e.g. CEdit is not longer derived directly from CWnd but from CWndEx. This way I could add a new member function to CWndEx so all the my controls would inherit this function? Seems to be a newbie :confused: question, yes? Thank you very much...
-
Hello, I would like to add a new member-function to all the controls of different kind in my dialog, e.g. CEdit, CButton and so on. Since they are all derived from CWnd I would like to know if there is a possibility to insert a new class "CWndEx" between CWnd and the control class, so that e.g. CEdit is not longer derived directly from CWnd but from CWndEx. This way I could add a new member function to CWndEx so all the my controls would inherit this function? Seems to be a newbie :confused: question, yes? Thank you very much...
No, this is not directly possible in the C++ language. I've wanted to do this myself on occasion along with making some necessary functions virtual that for some inexplicable reason are not. But there are ways around it to get the job done. Depending on what you'd like to achieve the simplest thing you can do is just to use a global function that takes a CWnd as a parameter. You won't be able to access protected members, but for most cases this generic function should be able to achieve what you want. It's not as pretty certainly, but the most important thing is to get the program to work. Another approach (at least for objects you directly create) would be to multiply inherit. This wouldn't apply to objects that are dynamically created, however.
-
No, this is not directly possible in the C++ language. I've wanted to do this myself on occasion along with making some necessary functions virtual that for some inexplicable reason are not. But there are ways around it to get the job done. Depending on what you'd like to achieve the simplest thing you can do is just to use a global function that takes a CWnd as a parameter. You won't be able to access protected members, but for most cases this generic function should be able to achieve what you want. It's not as pretty certainly, but the most important thing is to get the program to work. Another approach (at least for objects you directly create) would be to multiply inherit. This wouldn't apply to objects that are dynamically created, however.
Bill Buklis wrote: Another approach (at least for objects you directly create) would be to multiply inherit. This wouldn't apply to objects that are dynamically created, however. Actually I was trying to get this work for three days, I derived classes from CEdit and CButton and both from my own class "CInputCtrl" too, like this: class CEditEx : public CEdit, public CInputCtrl { public: void MyFunc(); } class CInputCtrl { public: void MyFunc(); } But now, when running this code CInputCtrl* pCtrl = (CInputCtrl*)GetFocus(); pCtrl->MyFunc(); the CInputCtrl::MyFunc() is called. If only I was able to "virtualize" the MyFunc() of CEditEx everything was OK. Can I change something so it works the way I expect? I already read about multiple inheritance but could not find something helping me further on... Thank you very much again...
-
Bill Buklis wrote: Another approach (at least for objects you directly create) would be to multiply inherit. This wouldn't apply to objects that are dynamically created, however. Actually I was trying to get this work for three days, I derived classes from CEdit and CButton and both from my own class "CInputCtrl" too, like this: class CEditEx : public CEdit, public CInputCtrl { public: void MyFunc(); } class CInputCtrl { public: void MyFunc(); } But now, when running this code CInputCtrl* pCtrl = (CInputCtrl*)GetFocus(); pCtrl->MyFunc(); the CInputCtrl::MyFunc() is called. If only I was able to "virtualize" the MyFunc() of CEditEx everything was OK. Can I change something so it works the way I expect? I already read about multiple inheritance but could not find something helping me further on... Thank you very much again...
With the code listed, you created two completely separate functions: CEditEx::MyFunc and CInputCtrl::MyFunc. If you have a pointer to a CInputCtrl, it will call CInputCtrl::MyFunc. To use multiple inheritance, what you want is for the code on the 2nd derivation to be more generic and not window oriented. Ths second branch should not be derived from CWnd, CObject, etc. Sort of like this: class CGeneric { virtual bool DoSomething(); } class CMyEdit : public CEdit, CGeneric { bool DoSomething(); }; The CGeneric class won't have direct access to any windowing features. It's similar to using the global functions, but with more inheritance control.