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.