Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

static function pointer to non-static function member



  • I look for a piece of advise:
    I need to trigger a non-static function member of my MyClass when any of the 256 objects of type MeterClass changes. I am trying by using a static function pointer but I am not able to call a non-static function in object "programa" of type MyClass.
    Function I need to call is in fact a non-static member of other class. I tryed pointing to a global function and it worked ok but now, I need to point to a non-static member of other class but it does not compile.

    class MeterClass{
    public:
       void setValue(unsigned long x){
          m = x;
          cb_update(); // here I call pointed func when value changes
       }
       static void (* cb_update)(void); //pointer to func to call when meter changes
    private:
       unsigned long m;
    }
    class MyClass{
    public:
       MeterClass meter[256]; // create 256 instances of MeterClass
       void objectHasBeenUpdated(void){ /* action */ } // non-static function to call when a meter changes
    //... code follow describing behaviour, at any time, one meter changes, let's say meter[10].setValue(5);
    }
    
    int main(int argc, char *argv[])
    {
       QApplication app(argc, argv);
       MyClass programa;
       MeterClass::cb_update = programa.objectHasBeenUpdated; // <--this does not work, does not compile
       return app.exec();
    }
    

    I am sorry if it is not clear, I dont want to be so extensive. Should I try with other resources? signals and slots?
    I hope anybody could advise.
    Thank you very much


  • Qt Champions 2017

    @ivanperino
    Hello,
    Static functions and global functions can be called the same way. Non-static functions (members) can be called through the pointer-to-member operators, but you need an object in addition to the member pointer.

    Look also here.

    Kind regards.


Log in to reply