Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Possible? Injection of a CWndEx-Class in MFC-structure?

Possible? Injection of a CWndEx-Class in MFC-structure?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ClockDivider
    wrote on last edited by
    #1

    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...

    L G 2 Replies Last reply
    0
    • C ClockDivider

      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...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      i thought of extending this in CEdit Class.. so not a newbie.. he he..;P

      1 Reply Last reply
      0
      • C ClockDivider

        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...

        G Offline
        G Offline
        GKarRacer
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • G GKarRacer

          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.

          C Offline
          C Offline
          ClockDivider
          wrote on last edited by
          #4

          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...

          G 1 Reply Last reply
          0
          • C ClockDivider

            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...

            G Offline
            G Offline
            GKarRacer
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups