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. How to write a common callback function?
Forum Updated to NodeBB v4.3 + New Features

How to write a common callback function?

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 2.8k 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by Mr Pang
    #1

    I have below code,

    #include <QCoreApplication>
    #include <QDebug>
    class Base
    {
      public:
        virtual void fun1() = 0;
    };
    class AAA: public Base
    {
    public:
        void fun1(){
            qWarning()<<__FUNCTION__<<__LINE__;
        }
    };
    
    class BBB: public Base
    {
    public:
        void fun1(){
            qWarning()<<__FUNCTION__<<__LINE__;
        }
    };
    
    typedef void (Base::*fun1)(void);
    class CCC
    {
    public:
        void run(fun1 f){
            f();
        }
    };
    
    int main(int argc, char *argv[])
    {
    
        AAA aaa;
        BBB bbb;
        CCC ccc;
        ccc.run(aaa.fun1); //run AAA::fun1
        ccc.run(bbb.fun1); //run BBB::fun1
        QCoreApplication a(argc, argv);
    
        return a.exec();
    }
    
    

    My purpose is that CCC::run() can call both AAA::fun1() and BBB::fun1(), but it doesn't compile.
    What is wrong?

    Thanks!

    JKSHJ 1 Reply Last reply
    0
    • M Mr Pang

      I have below code,

      #include <QCoreApplication>
      #include <QDebug>
      class Base
      {
        public:
          virtual void fun1() = 0;
      };
      class AAA: public Base
      {
      public:
          void fun1(){
              qWarning()<<__FUNCTION__<<__LINE__;
          }
      };
      
      class BBB: public Base
      {
      public:
          void fun1(){
              qWarning()<<__FUNCTION__<<__LINE__;
          }
      };
      
      typedef void (Base::*fun1)(void);
      class CCC
      {
      public:
          void run(fun1 f){
              f();
          }
      };
      
      int main(int argc, char *argv[])
      {
      
          AAA aaa;
          BBB bbb;
          CCC ccc;
          ccc.run(aaa.fun1); //run AAA::fun1
          ccc.run(bbb.fun1); //run BBB::fun1
          QCoreApplication a(argc, argv);
      
          return a.exec();
      }
      
      

      My purpose is that CCC::run() can call both AAA::fun1() and BBB::fun1(), but it doesn't compile.
      What is wrong?

      Thanks!

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @Mr-Pang said in How to write a common callback function?:

      typedef void (Base::*fun1)(void);
      class CCC
      {
      public:
          void run(fun1 f){
              f();
          }
      };
      

      ...

      My purpose is that CCC::run() can call both AAA::fun1() and BBB::fun1()

      Base::fun1() is already a virtual method, so you should use C++ polymorphism:

      class CCC
      {
      public:
          void run(Base *b){
              b->fun1();
          }
      };
      
      int main(int argc, char *argv[])
      {
          AAA aaa;
          BBB bbb;
          CCC ccc;
          ccc.run(&aaa);
          ccc.run(&bbb);
      }
      

      it doesn't compile.
      What is wrong?

      Your compiler error message tells you what is wrong.

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

      M 1 Reply Last reply
      4
      • JKSHJ JKSH

        @Mr-Pang said in How to write a common callback function?:

        typedef void (Base::*fun1)(void);
        class CCC
        {
        public:
            void run(fun1 f){
                f();
            }
        };
        

        ...

        My purpose is that CCC::run() can call both AAA::fun1() and BBB::fun1()

        Base::fun1() is already a virtual method, so you should use C++ polymorphism:

        class CCC
        {
        public:
            void run(Base *b){
                b->fun1();
            }
        };
        
        int main(int argc, char *argv[])
        {
            AAA aaa;
            BBB bbb;
            CCC ccc;
            ccc.run(&aaa);
            ccc.run(&bbb);
        }
        

        it doesn't compile.
        What is wrong?

        Your compiler error message tells you what is wrong.

        M Offline
        M Offline
        Mr Pang
        wrote on last edited by
        #3

        @JKSH
        Great! I never thought of it before.

        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