Results 1 to 6 of 6

Thread: Function pointers, class member functions

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Resolved Function pointers, class member functions

    I have 2 classes (both derived but i don't think that matters).

    One class is a Button class, and has a function pointer that is triggered when it's clicked.

    My other class is a MessageBox class. This creates 2 instances of the MessageBox. It also has a few member functions such as void WM_ClickedYes(void).

    I'm trying to assign this member function to the pointer through the Init member function of my Button class, which excepts a function address/pointer (void(*ClickFunction)(void)).

    This is how i'm trying to pass in the member function:

    &CGUIMessageBox::WM_ClickedOk

    It gives me the following error:

    error C2664: 'CGUIButton::Init' : cannot convert parameter 8 from 'void (__thiscall CGUIMessageBox::* )(void)' to 'void (__cdecl *)(void)'
    I've tried numerous things such as putting (void*) at the start, putting brackets all over the place, but i just can't get it to work.

    Any ideas?
    Last edited by SLH; Sep 24th, 2004 at 01:18 PM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A member function is different from a normal function. They are not pointer compatible. A pointer that you could assign MessageBox::WM_YesClicked to would be:

    typedef void (MessageBox::*ClickFunction)();
    ClickFunction ptr = MessageBox::WM_YesClicked;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Thanks for the explanation and example code.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    The reason for this is that your member function uses the "thiscall" calling convention (or if you use var args, the c calling convention with a hidden argument); like CornedBee said, you cannot cast a pointer to a member function to a regular function pointer.

    What's going on behind the scenes is every time you call a member function, that function has to know which instance of the object it belongs to; to resolve this, the "thiscall" calling convention passes a pointer to the object (*this) as the first argument to the function.

    You could instead pass pointer to a static member function, and then from that static member function determine which instance of your object needs to receive the message, and call that function.

    *phew*
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    My way is easier
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Thanks for the in-depth clear explanation sunburnt, but yeah, CornedBee's method is suitable for my needs
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width