Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. static function pointer to non-static function member
Forum Update on Monday, May 27th 2025

static function pointer to non-static function member

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views
  • 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.
  • I Offline
    I Offline
    ivanperino
    wrote on 7 May 2016, 00:35 last edited by ivanperino 5 Jul 2016, 00:42
    #1

    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

    K 1 Reply Last reply 7 May 2016, 03:07
    0
    • I ivanperino
      7 May 2016, 00:35

      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

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 7 May 2016, 03:07 last edited by
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0

      1/2

      7 May 2016, 00:35

      • Login

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