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. C++ How do I create and store a function pointer that someone could use as a callback?
Forum Updated to NodeBB v4.3 + New Features

C++ How do I create and store a function pointer that someone could use as a callback?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.6k 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.
  • A Offline
    A Offline
    AI_Messiah
    wrote on 6 Dec 2022, 19:35 last edited by
    #1

    I want to build an interpreter. I need a way for people to be able to use my code and define their own functions. I would also like to be able to pass this from another class. This is what I have.

    //LineEval.h
    
    #include <vector>
    #include <functional>
    #include <string>
    
    class LineEval
    {
    public:
    	void setFunction(std::string name, std::function<outard(inargs)> &address);
    private:
    	std::vector<std::function<outard(inargs)>> ops;
    	std::vector<std::string> funNames;
    }
    
    //LineEval.cpp
    void LineEval::setFunction(std::string name, std::function<outard(inargs)> &address)
    {
    	funNames.push_back(name);
    	ops.push_back(address);
    	
    	
    }
    
    //Source.cpp
    #include "LineEval.h"
    
    outard sin(inargs) {
    
    }
    
    int main()
    {
        LineEval eval;
        eval.setFunction("sin", sin);
    }
    
    J 1 Reply Last reply 7 Dec 2022, 07:23
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 6 Dec 2022, 22:47 last edited by
      #2

      Apart from having no relationship to Qt...

      What is the problem you are experiencing?
      Does your code compile? If not, what are the errors etc.
      If so, does it behave differently to expectations? How so?

      1 Reply Last reply
      1
      • A AI_Messiah
        6 Dec 2022, 19:35

        I want to build an interpreter. I need a way for people to be able to use my code and define their own functions. I would also like to be able to pass this from another class. This is what I have.

        //LineEval.h
        
        #include <vector>
        #include <functional>
        #include <string>
        
        class LineEval
        {
        public:
        	void setFunction(std::string name, std::function<outard(inargs)> &address);
        private:
        	std::vector<std::function<outard(inargs)>> ops;
        	std::vector<std::string> funNames;
        }
        
        //LineEval.cpp
        void LineEval::setFunction(std::string name, std::function<outard(inargs)> &address)
        {
        	funNames.push_back(name);
        	ops.push_back(address);
        	
        	
        }
        
        //Source.cpp
        #include "LineEval.h"
        
        outard sin(inargs) {
        
        }
        
        int main()
        {
            LineEval eval;
            eval.setFunction("sin", sin);
        }
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Dec 2022, 07:23 last edited by
        #3

        @AI_Messiah said in C++ How do I create and store a function pointer that someone could use as a callback?:

        std::function<outard(inargs)> &address

        You are passing a reference to a function, not a pointer.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AI_Messiah
          wrote on 8 Dec 2022, 23:13 last edited by
          #4

          I worked it out and this is how I did it:

          //Class.h
          #pragma once
          #include <string>
          #include <functional>
          #include <vector>
          #include <iostream>
          using namespace std;
          class Class
          {
          public:
          	void run1();
          	void setFunction(string name, function<int(int)> fun);
          private:
          	vector<string> names;
          	vector<function<int(int)>> functs;
          };
          
          //Class.cpp
          #include "Class.h"
          
          void Class::run1()
          {
          	int aval = functs[0](23);
          	cout << aval << endl;
          }
          
          void Class::setFunction(string name, function<int(int)> fun)
          {
          	names.push_back(name);
          	functs.push_back(fun);
          }
          

          I would like to be able to pass a class member though.

          C J 2 Replies Last reply 9 Dec 2022, 06:21
          0
          • A AI_Messiah
            8 Dec 2022, 23:13

            I worked it out and this is how I did it:

            //Class.h
            #pragma once
            #include <string>
            #include <functional>
            #include <vector>
            #include <iostream>
            using namespace std;
            class Class
            {
            public:
            	void run1();
            	void setFunction(string name, function<int(int)> fun);
            private:
            	vector<string> names;
            	vector<function<int(int)>> functs;
            };
            
            //Class.cpp
            #include "Class.h"
            
            void Class::run1()
            {
            	int aval = functs[0](23);
            	cout << aval << endl;
            }
            
            void Class::setFunction(string name, function<int(int)> fun)
            {
            	names.push_back(name);
            	functs.push_back(fun);
            }
            

            I would like to be able to pass a class member though.

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 9 Dec 2022, 06:21 last edited by
            #5

            @AI_Messiah said in C++ How do I create and store a function pointer that someone could use as a callback?:

            I would like to be able to pass a class member though.

            This can't work since then the context is missing. You can pass a lmabda which cataches the class instance and calls the function though.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • A AI_Messiah
              8 Dec 2022, 23:13

              I worked it out and this is how I did it:

              //Class.h
              #pragma once
              #include <string>
              #include <functional>
              #include <vector>
              #include <iostream>
              using namespace std;
              class Class
              {
              public:
              	void run1();
              	void setFunction(string name, function<int(int)> fun);
              private:
              	vector<string> names;
              	vector<function<int(int)>> functs;
              };
              
              //Class.cpp
              #include "Class.h"
              
              void Class::run1()
              {
              	int aval = functs[0](23);
              	cout << aval << endl;
              }
              
              void Class::setFunction(string name, function<int(int)> fun)
              {
              	names.push_back(name);
              	functs.push_back(fun);
              }
              

              I would like to be able to pass a class member though.

              J Offline
              J Offline
              JoeCFD
              wrote on 9 Dec 2022, 19:07 last edited by
              #6

              @AI_Messiah in C++ classes are used and functions are defined inside classes. It may be better to store class pointers and call their funcs back instead of using function pointers(very C style).

              1 Reply Last reply
              1

              1/6

              6 Dec 2022, 19:35

              • Login

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