Thread: Assign class action using function pointers....

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Assign class action using function pointers....

    Hello, I'm still a beginner with C++, while being "well trained" with C. I'm developing a class for a customizable GUI control, which lets you assign a Paint function.

    To do that I use function pointers:
    Code:
    typedef struct
    {
      CPos pos;
      ...
    }PaintFuncParamsType;
    
    typedef bool (PAINTFUNC*)( PaintFuncParamsType* pParam );
    
    class myButton
    {
    ...
    public:
    void SetPaintFunc( PAINTFUNC f ){if( f ) m_pf = f;};
    void Draw();
    ....
    private:
    PAINTFUNC m_pf;
    CPos m_pos;  //position of the control and size
    }
    
    void myButton::Draw()
    {
      PaintFuncParamsType p;
      
      p.pos = m_pos;  //assume "operator =" defined
      return (*m_pf)( &p );
    }
    This seems to work, but I want to know if there's a better way to do it using C++ features.

    Also, there are many limitations with this solution, because I can't access to class private members from the called functions, I need to pass them to the called "custom" function using as parameter a pointer to a structured type, as in the previous example.

    Thank for any help and suggestion!
    BrownB

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Inheritance and subtype polymorphism, virtual functions. That's what you're looking for. But it's a big topic - too big to be explained in a forum post.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by CornedBee View Post
    Inheritance and subtype polymorphism, virtual functions. That's what you're looking for. But it's a big topic - too big to be explained in a forum post.
    What about an example on my previous code? that would be very useful for me!I know something about those OOP topics, but can't see how to use them in this case...perhaps I should feel a bit shame..?:P

    Thank you.
    BrownB

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are several ways to do a GUI system but it also depends on what API, if any, you are using in the underlying code. If it's for Windows MFC is a very good, albeit quite confusing, example of such a class hierarchy. But pouring over MFC source code will probably make you want to throw your computer out the window so I don't recommend trying it.

    If it were me I'd start with a base class called CWindow and then derive others from it. A button is a window, but so is an edit control, etc, etc. But laying out a class framework here is hopeless without understanding what you are doing with it or plan to do. The class must be tied into graphics classes to draw the windows, buttons, etc as well as probably tied into an application class, doc/view classes, etc, etc. The list goes on and on.

    So since the topic of 'building a GUI class framework' is so broad we probably need a bit more understanding of what it is that you want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM