Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is it possible to have callback function as a class member?

Is it possible to have callback function as a class member?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aidman
    wrote on last edited by
    #1

    Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

    F A N 3 Replies Last reply
    0
    • A Aidman

      Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

      F Offline
      F Offline
      FlyingDancer
      wrote on last edited by
      #2

      I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.

      A F 2 Replies Last reply
      0
      • A Aidman

        Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

        A Offline
        A Offline
        Andrew Walker
        wrote on last edited by
        #3

        Boost can let you do this. Here's a starting point, but for a much better example download boost and look at the sample code for boost::thread http://www.codeproject.com/vcpp/stl/boostintro.asp#xx563708xx[^]


        If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

        1 Reply Last reply
        0
        • A Aidman

          Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #4

          This is standard C++ stuff which you should find in any good C++ Reference book. Here is a code snippet: class CBookmarks { public: void ScanBookmarks( void (CBookmarks::*Scan_Func)( const BMRK, const ulong, const ulong ), const ulong var1, const ulong var2, const LN_NUM ln_num ); }; // Function definition void CBookmarks::ScanBookmarks( void (CBookmarks::*Scan_Func)( const BMRK, const ulong, const ulong ), const ulong var1, const ulong var2, const LN_NUM ln_num ) { } // Function using callback. bits CBookmarks::ClmChanged( const LN_NUM ln_num, const LN_CLM clm, const int dir ) { m_changed_bookmarks = 0x00; ScanBookmarks( _ClmChanged, clm, dir, ln_num ); return m_changed_bookmarks; } // Callback function void CBookmarks::_ClmChanged( const BMRK mp, const LN_CLM clm, const ulong dir ) { if ( mp->m_ln_clm >= clm ) { mp->m_ln_clm += dir; MASKON( mp->m_status, MOVED ); // flag that the bookmark has moved BITON( m_changed_bookmarks, mp - m_bookmarks ); mp->m_ln_oclm = MARK_OFF; // reset as no longer valid } } Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

          1 Reply Last reply
          0
          • F FlyingDancer

            I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.

            A Offline
            A Offline
            Aidman
            wrote on last edited by
            #5

            Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

            R J 2 Replies Last reply
            0
            • A Aidman

              Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

              R Offline
              R Offline
              Rage
              wrote on last edited by
              #6

              Usually you can pass a pointer on your main window to the callback function. If the parameter of the callback function is LPVOID, you will have to typecast it to your window type. if it looks like

              CMyDialog::callbackfunction(... , LPVOID pointer)

              use it like this :

              callbackfuntionc(...,this);

              and in the function

              CMyDialog::callbackfunction(... , LPVOID pointer)
              {
              CMyDialog *pMain=(CMyDialog*)pointer;
              pMain->m_variable=0; // here you can access them...
              }

              ~RaGE();

              1 Reply Last reply
              0
              • F FlyingDancer

                I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.

                F Offline
                F Offline
                FlyingDancer
                wrote on last edited by
                #7

                You can define a static varible member in the class including callback function if something has happened you can change the varible of course it is an indirect way. :)

                1 Reply Last reply
                0
                • A Aidman

                  Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  static function can not access non-static members. Look at the Code Project FAQs for anouther way to implement callback functions. INTP

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups