C++ How do I create and store a function pointer that someone could use as a callback?
-
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); }
-
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.
-
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.
@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).