Thread: Class inheritance, function pointers problem.

  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 06: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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    they have different type for the first (hidden) parameter

    Why not to make it just a virtual function instead of these games with pointers?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    I cannot use virtual functions for what im doing.
    So, what are you trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 06:45 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 08: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, 11:03 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02: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, 01:18 PM