How do I create a function pointer that someone could use for their own implementation from another class and store it?

Paul Arnold 1 Reputation point
2022-12-06T01:33:10.017+00:00

What I want is to make an interpreter. I want to implement a way for people to define their own functions. I think this would also be used for functions that should be included like 'sin()' anyway this is the code in the form of the relevant parts:

//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);  
}//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);  
}  

I also might want to call 'setFunction()' from another class.

Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2022-12-06T06:08:29.003+00:00

    Hi, @Paul Arnold

    You can refer to the snippet below:

     //LineEval.h  
          
     #include <vector>  
     #include <functional>  
     #include <string>  
      
    class LineEval  
    {  
    public:  
        void setFunction(std::string name, std::function<void(int)> address);  
        void playFunction(int pos,int para);  
        void playFunction(std::string FunName,int para);  
      
    private:  
        std::vector<std::function<void(int)>> ops;  
        std::vector<std::string> funNames;  
    };  
      
      
      
    //LineEval.cpp  
    #include"LineEval.h"  
    void LineEval::setFunction(std::string name, std::function<void(int)> address)  
    {  
        funNames.push_back(name);  
        ops.push_back(address);  
      
    }  
        void LineEval::playFunction(int pos,int para)  
      {  
            ops[pos](para);  
        }  
        void LineEval::playFunction(std::string FunName,int para)  
        {  
              
            auto it = std::find(funNames.begin(), funNames.end(), FunName);  
            if(it!= funNames.end())  
                ops[std::distance(funNames.begin(), it)](para);  
               
            else {}  
        }  
    
    //Source.cpp  
    #include<iostream>  
     #include "LineEval.h"  
    void  sinX (int para) {  
        std::cout << "hello"<< para<<std::endl;  
    }  
      
      
    int main()  
    {  
        LineEval eval;  
        eval.setFunction("sinX", sinX);  
        eval.playFunction("sinX",23);  
        eval.playFunction(0, 123);  
      
    }  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.