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. Function references

Function references

Scheduled Pinned Locked Moved C / C++ / MFC
c++regexhelptutorialquestion
6 Posts 3 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.
  • D Offline
    D Offline
    Dimitris Vikeloudas
    wrote on last edited by
    #1

    I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like #include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); } And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it??

    T S 2 Replies Last reply
    0
    • D Dimitris Vikeloudas

      I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like #include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); } And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it??

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      as notify() is a member of a class, then it implicitely receives the this pointer as a parameter... to solve this, declare it like this :

      static void notify(TBK_BOOL note);


      TOXCCT >>> GEII power
      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

      1 Reply Last reply
      0
      • D Dimitris Vikeloudas

        I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like #include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); } And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it??

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        You can't (as you've seen) assign a pointer to a member function to a normal function pointer. A pointer to a member function would look like this: typedef void (Listener::*)(TBK_BOOL note) NEW_LISTENER_NOTIFICATION_FUNCTION; To call this pointer you would need more information then with a normal function pointer, you need to supply the this pointer. A call could look like this: Listener x; NEW_LISTENER_NOTIFICATION_FUNCTION pFunc = &Listener::notify; (x.*pFunc)(Some_Value); The functionality you're after seems more like a delegate then a C++ style member pointer. I believe there is an article on the code project which provides such delegates - but these can only be used from C++. Steve

        T 1 Reply Last reply
        0
        • S Stephen Hewitt

          You can't (as you've seen) assign a pointer to a member function to a normal function pointer. A pointer to a member function would look like this: typedef void (Listener::*)(TBK_BOOL note) NEW_LISTENER_NOTIFICATION_FUNCTION; To call this pointer you would need more information then with a normal function pointer, you need to supply the this pointer. A call could look like this: Listener x; NEW_LISTENER_NOTIFICATION_FUNCTION pFunc = &Listener::notify; (x.*pFunc)(Some_Value); The functionality you're after seems more like a delegate then a C++ style member pointer. I believe there is an article on the code project which provides such delegates - but these can only be used from C++. Steve

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          as his function doesn't modify the class itself, he'd better set the function static...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

          S 1 Reply Last reply
          0
          • T toxcct

            as his function doesn't modify the class itself, he'd better set the function static...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            It doesn't modify the class but it does access a member variable. I assumed that in the real implementation the function would be more elaborate. Steve

            T 1 Reply Last reply
            0
            • S Stephen Hewitt

              It doesn't modify the class but it does access a member variable. I assumed that in the real implementation the function would be more elaborate. Steve

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              :-O sorry, i didn't see the this->identity...


              TOXCCT >>> GEII power
              [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

              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