C++ How do I create and store a function pointer that someone could use as a callback?
-
wrote on 6 Dec 2022, 19:35 last edited by
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); }
-
wrote on 6 Dec 2022, 22:47 last edited by
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? -
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); }
@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.
-
wrote on 8 Dec 2022, 23:13 last edited by
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.
-
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.
@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.
-
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.
wrote on 9 Dec 2022, 19:07 last edited by@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/6