Assign class action using function pointers....

This is a discussion on Assign class action using function pointers.... within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm still a beginner with C++, while being "well trained" with C. I'm developing a class for a customizable ...

  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,893
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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, 09:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05: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-17-2006, 12:35 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM

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