Class inheritance, function pointers problem.

This is a discussion on Class inheritance, function pointers problem. within the C++ Programming forums, part of the General Programming Boards category; I have a class than inherits from a base class. One of the members of the base class is a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Class inheritance, function pointers problem.

    I have a class than inherits from a base class. One of the members of the base class is a function pointer. When i try to set the address of this pointer to one of the functions within the ancestors class i get an error.

    Example:
    Code:
    class CBase
    {
    protected:
      int (*onSomeEvent)();
    };
    
    class CAncestor:
      public CBase
    {
    public:
      
      int onDoubleClick()
      {
        return 1;
      }
    
      CAncestor()
      {
        onSomeEvent = &CAncestor::onDoubleClick;
      }
    };
    Error 7 error C2440: '=' : cannot convert from 'int (__thiscall CDlgMain::* )(void)' to 'int (__thiscall *)(void)' d:\code\projects\editor\src\DlgMain.h 12

    If i define the function pointer inside the ancestor class instead of the base class it will work

    Code:
    class CAncestor:
      public CBase
    {
    public:
      
      int onDoubleClick()
      {
        return 1;
      }
    
      CAncestor()
      {
         int (CAncestor::*onSomeEvent)() = &CAncestor::onDoubleClick;
      }
    };
    Last edited by 39ster; 02-14-2008 at 05:12 AM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    You are trying to assign a member function pointer to a function pointer.

    You should use the initializer list by the way.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    they have different type for the first (hidden) parameter

    Why not to make it just a virtual function instead of these games with pointers?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Anyone know how i can get it to work? I tried this to make it a member function pointer:

    Code:
    class CBase
    {
    protected:
      int (CBase::*onSomeEvent)();
    };
    but it doesnt work!

    Btw what is a initialize list? Is it this:

    Code:
      CAncestor(): CBase()
      {
         ....
      }
    @vart It is no game with pointers. I cannot use virtual functions for what im doing.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    I cannot use virtual functions for what im doing.
    So, what are you trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Im trying to create a windows class with event handlers (the same way C# does it). The reason i cannot just do this:

    Code:
    class CWindowClass
    {
      HWND handle;
    public:
      virtual int onMouseMove(){}
    };
    Is because the CWindowClass (which is the base class) will not always be inherited. The only time it will be inherited is for actual windows. Other controls such as buttons, panels, etc will still be encapsulated in the CWindowClass. In order to control events such as mouse moving over a button i need to use function pointers. Heres an example:

    Code:
    class CMyMainWindow:
      public CWindowClass
    {
    public:
      CMyMainWindow(HWND hwnd): CWindowClass(hWnd)
      {
         CWindowClass statusBar(CreateWindow(.....));
         statusBar.onMouseMovePtr = &CMyMainWindow::statusbar_onMouseMove;
      }
     
      int statusbar_onMouseMove() 
      {
         return 1;
      }
     
    };
    Last edited by 39ster; 02-14-2008 at 05:45 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Is because the CWindowClass (which is the base class) will not always be inherited. The only time it will be inherited is for actual windows. Other controls such as buttons, panels, etc will still be encapsulated in the CWindowClass. In order to control events such as mouse moving over a button i need to use function pointers.
    I might suggest a CControlClass abstract base class which is subclassed by CWindowClass, upon which your actual windows can subclass CWindowClass, but the other controls subclass CControlClass directly.

    However, it looks like you will then define a virtual function of the onX() variety for each possible action, and this could be rather cumbersome, especially since not all actions would make sense for all controls. On a hunch, would say, the decorator pattern be useful here?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Ok now that i thought about it some more i realise it woudnt work even if i did get the function pointers to work because there is no way the statusbar would know which instance of CMyMainClass to use when it calls onMouseMovePtr().

    I guess i have to do something like this:

    Code:
    CWindowClass statusBar(CreateWindow(.....));
    statusBar.onMouseMovePtr = CMessageHandler(&CMyMainWindow::statusbar_onMouseMove, this);
    that way it provides the function address and the object. Still need to figure out how to set the
    function pointer.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Ok i figured it out. For anyone else who needs it, it looks like this:

    Code:
    #include <cstdio>
    #define MessageHandler(a, b) CMessageHandler((int (CControlClass::*)())(a), b)
    
    class CControlClass;
    class CMessageHandler
    {
    public:
      int (CControlClass::*funcPtr)();
      CControlClass* controlPtr;
    
      CMessageHandler()
      {
          funcPtr = 0;
          controlPtr = 0;
      }
    
      CMessageHandler(int (CControlClass::*pFuncPtr)(), CControlClass* pControlPtr)
      {
          funcPtr = pFuncPtr;
          controlPtr = pControlPtr;
      }
    
      int call()
      {
          return (*controlPtr.*funcPtr)();
      }
    
      bool isSet()
      {
          return funcPtr != 0;
      }
    };
    
    class CControlClass
    {
    public:
        CMessageHandler onMouseMoveHandle;
    
        int callMouseMove()
        {
            return onMouseMoveHandle.call();
        }
    };
    
    class CWindowClass:
        public CControlClass
    {
    
    };
    
    class CMyWindow:
        public CWindowClass
    {
    public:
    
        int onMouseMove()
        {
            printf("Hello\n");
            return 5;
        }
    };
    
    
    
    int main(int argc, char* argv[])
    {
        CMyWindow window;
        window.onMouseMoveHandle = MessageHandler(&CMyWindow::onMouseMove, &window);
        printf("&#37;i\n", window.callMouseMove());
    }
    Last edited by 39ster; 02-19-2008 at 07:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of function pointers in a class
    By mlupo in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2009, 03:36 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21