Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pointer to C++ function
Forum Updated to NodeBB v4.3 + New Features

Pointer to C++ function

Scheduled Pinned Locked Moved C++ Gurus
11 Posts 6 Posters 6.4k Views 2 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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on last edited by
    #2

    Hi, if you make OnData() static you can do it.

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #3

      I know but if I declare as static the function I can't use non static objects as qvector or QDateTime, declared in the class...

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #4

        You could create static copies of your qvector and QDateTime objects as well.

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #5

          The object has an own QVector, I have to append data to it. Is there a way?

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #6

            Hi,

            No you cannot pass a pointer-to-non-static-member function, because its type is different. See http://www.parashift.com/c++-faq/fnptr-vs-memfnptr-types.html

            • Your C function accepts pointers of type void (*)(void), but
            • Your member function's type is void (QThlpt::*)(void)

            If your compiler supports C++11, you can use std::bind() or a lambda to create a function of type void (*)(void) that modifies your C++ object. Then, you can pass this function to wiringPiISR()

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            1
            • mrdebugM Offline
              mrdebugM Offline
              mrdebug
              wrote on last edited by
              #7

              Solved modifing the exteran c code by passing to it a pointer to my object and a pojnter to my function.

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LuisCarl
                wrote on last edited by
                #8

                [quote author="JKSH" date="1412506264"]
                If your compiler supports C++11, you can use std::bind() or a lambda to create a function of type void (*)(void) that modifies your C++ object. Then, you can pass this function to wiringPiISR()[/quote]

                Could you give us an example of implementation?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Monni
                  wrote on last edited by Monni
                  #9

                  In case of someone else wanting to have wiringPiISR() function to work in a class, I wrestled a couple of days with this issue myself. I managed to create a dirty workaround (I know, ew, global pointer) for this, and want to share it to someone who looks for the same thing:

                  Create static method with your normal method within MyISRClass.h:

                  public: 
                  void myFunction();
                  static void isrCatcher();
                  

                  In main.cpp, create global pointer to remember the instance the ISR initialization is in:

                  MyISRClass *isrClass = 0;
                  

                  Store the address of the class instance:

                  extern MyISRClass *isrClass
                  MyISRClass::MyISRClass()
                  {
                    ...
                    isrClass = this;
                    wiringPiISR(pin, INT_EDGE_BOTH, isrCatcher);
                    ...
                  }
                  
                  void MyISRClass ::isrCatcher()
                  {
                    isrClass->myFunction();
                  }
                  

                  I hope this helps someone who comes to look for the same answer. I'm a noob with c++, so this probably isn't the safest or even secure way to do this, but it works if you just have to get that Raspberry Pi program to work with simple knowledge.

                  And don't forget to include LIBS += -L/usr/local/include -lwiringPi -line to your .pro file when compiling.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Konstantin Tokarev
                    wrote on last edited by
                    #10

                    Common pattern for using C callbacks with C++ classes is passing static method (or internal static free function) as a callback pointer, and using this as a "data" pointer which is usually passed into callback as void* argument when it's called (in your callback code you cast it into pointer to your class and proceed)

                    static ReturnType A::callbackMethod(<arguments>, void *data)
                    {
                        A* self = static_cast<A*>(data);
                        // Following code uses self instead of this
                        // ...
                    }
                    1 Reply Last reply
                    3
                    • mrdebugM Offline
                      mrdebugM Offline
                      mrdebug
                      wrote on last edited by
                      #11

                      Many thanks for your post but this thread is very outdated.
                      Regards.

                      Need programmers to hire?
                      www.labcsp.com
                      www.denisgottardello.it
                      GMT+1
                      Skype: mrdebug

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved