Callback function as class method

This is a discussion on Callback function as class method within the Windows Programming forums, part of the Platform Specific Boards category; Originally Posted by matsp Code: D:\temp>g++ -Wall -Wextra -ansi -pedantic fun.cpp D:\temp>cat fun.cpp #include <iostream> class foo { public: static ...

  1. #31
    Registered User
    Join Date
    May 2008
    Posts
    13
    Quote Originally Posted by matsp View Post
    Code:
    D:\temp>g++ -Wall -Wextra -ansi -pedantic fun.cpp
    
    D:\temp>cat fun.cpp
    #include <iostream>
    
    class foo
    {
    public:
      static void bar(int x) { std::cout << x << std::endl; }
    };
    
    
    
    void fun(void (*f)(int x))
    {
      f(7);
    }
    
    int main()
    {
      fun(foo::bar);
      return 0;
    }
    Works just fine like that, no need for &.

    --
    Mats

    It works fine without &, also Elysia's suggestion also works fine too using the &.

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lakewa View Post
    matsp, thanks the error is gone. But may I know why it's necessary to assign it to the object itself.

    gPtrMyTest = this;
    MyTest *temp = gPtrMyTest;
    Because a static class member does not automatically get the this pointer (all normal method functions of a class do get the "this" pointer transferred by the compiler) , and you need it in your logging part. So you need a way to pass it from your call to DialogBox to the playTone() function. Since there is no parameter from within the DialogBox call that is freely available and gets passed, the only trivial solution is to use a global variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Registered User
    Join Date
    May 2008
    Posts
    13
    It writes perfectly to the log file as I check the contents in the file.

    Thank you everyone for the input to help me out, very appreciated.

  4. #34
    Registered User
    Join Date
    May 2008
    Posts
    13
    Quote Originally Posted by matsp View Post
    Because a static class member does not automatically get the this pointer (all normal method functions of a class do get the "this" pointer transferred by the compiler) , and you need it in your logging part. So you need a way to pass it from your call to DialogBox to the playTone() function. Since there is no parameter from within the DialogBox call that is freely available and gets passed, the only trivial solution is to use a global variable.

    --
    Mats
    Trying to use the static functioin but without having a complete understanding that is why I run into issue, thank you so much for your explanation.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lakewa View Post
    Trying to use the static functioin but without having a complete understanding that is why I run into issue, thank you so much for your explanation.
    I thought that was part of the problem.

    The other key point here is that the reason you NEED to make the function static is that there's no understanding from Windows' side that you need to pass a "this" (or what the value for "this" might be) when calling a CALLBACK function, so you must use a function that doesn't expect "this" to magically be passed along with the parameters to the function. Hence you must create your own mechanism of asking for the pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Quote Originally Posted by matsp View Post
    Works just fine like that, no need for &.
    I suspect that's because it's static. If I remove the static, I get a compile error, as expected:

    Code:
    error C3867: 'foo::bar': function call missing argument list; use '&foo::bar' to create a pointer to member
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Registered User
    Join Date
    May 2008
    Posts
    13
    Quote Originally Posted by matsp View Post
    I thought that was part of the problem.

    The other key point here is that the reason you NEED to make the function static is that there's no understanding from Windows' side that you need to pass a "this" (or what the value for "this" might be) when calling a CALLBACK function, so you must use a function that doesn't expect "this" to magically be passed along with the parameters to the function. Hence you must create your own mechanism of asking for the pointer.

    --
    Mats

    Ok hehehe that one I don't know too. I always program using console while in school to work on projects and now just shift to win32, it looks like a different language to me.

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lakewa View Post
    Ok hehehe that one I don't know too. I always program using console while in school to work on projects and now just shift to win32, it looks like a different language to me.
    Well, the entire Win32 API is written in C, not C++, and it uses callbacks quite extensively - and most of the callbacks do not have any spare parameters for the user to pass arbitrary extra arguments - that would have helped a whole lot.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I suspect that's because it's static. If I remove the static, I get a compile error, as expected:

    Code:
    error C3867: 'foo::bar': function call missing argument list; use '&foo::bar' to create a pointer to member
    Ok, that may be the case - I don't generally use function pointers from classes. It's mostly meaning that you should use more polymorphism to solve the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by matsp View Post
    and most of the callbacks do not have any spare parameters for the user to pass arbitrary extra arguments - that would have helped a whole lot.
    Actually, the Win32 API is really good about this. Nearly all the callbacks have a user data parameter. Not quite all of them, but most.
    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

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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