Function pointer in parameter
I need to make a function which adds a function pointer to map. It would be something like this:
bool RegisterFunction (string name, FunctionPointer function).
But I have problem with calling it, because I don't know how to pass function to it instead of result of function (when I call this as here:
RegisterFunction ("run", Run())
it doesn't works, neither works Run without parentheses, nor:
- *Run()
- &Run()
- *Run
- &Run
How to fix this?
Edit:
The error 开发者_StackOverflow中文版is:parser.cpp|9|error: no matching function for call to
'MCXJS::Parser::RegisterFunction(const char [4], <unresolved overloaded function type>)'|
The RegisterFunction() and Run() functions are in Parser class, which is in MCXJS namespace.
Class body is:class Parser
{
public:
Parser ();
CVariable RegisterFunction (FunctionPointer);
bool RegisterErrorHandler (ErrorType, ErrorHandlerPointer);
CVariable Run (std::string);
bool AlwaysDefaultErrorHandler;
int MaxCallStackSize;
private:
std::map <std::string, FunctionPointer> ExternalFunctions;
std::map <ErrorType, ErrorHandlerPointer> ErrorHandlers;
ErrorHandlerPointer DefaultErrorHandler;
};
And the parser.cpp file:
Parser::Parser ():
AlwaysDefaultErrorHandler (true), MaxCallStackSize (4)
{
RegisterFunction ("run", Run);
};
CVariable Parser::Run (std::string path)
{
return 5;
};
Typedefs:
typedef CVariable (*FunctionPointer) (std::string);
typedef void (*ErrorHandlerPointer) (ErrorData);
Run
is a non-static member function, not a normal function so either you are registering something of the wrong type or you need to change your typedef
to refer to a pointer-to-member.
E.g.
typedef CVariable (Parser::*FunctionPointer) (std::string);
Then the correct way to form a pointer-to-member would be:
RegisterFunction("run", &Parser::Run);
Note that you have to use either the .*
or ->*
operator with an object or object pointer respectively to call the member function through the pointer-to-member.
RegisterFunction ("run", Run)
is the correct method. What error are you getting using that?
I suspect that the problem is not with how you are calling RegisterFunction, but with how you are defining it. You give us this:
RegisterFunction (string name, FunctionPointer function).
but leave out the declaration of FunctionPointer
. It would need to be defined something like:
typedef void (*FunctionPointer)()
Assuming that Run
is defined as:
void Run();
Note that for this to work, All of the functions you use with RegisterFunction
must have the same signature.
UPDATE: Based on the error message you provided in the comment, it seem the problem is that you have more than one "Run" function, and the compiler don't know which one you want to pass. (Unfortunately, I'm not sure how you clarify it, so you may wish to rename one of them)
One thing you may want to try is:
RegisterFunction (std::string("run"), Run);
Given an exact match for the first parameter, it may be able to choose which Run
function based on which matches the signature in FunctionPointer
.
UPDATE2 : You will either need to make Parser::Run() a static function, are change the declaration of FunctionPointer to:
typedef CVariable (Parser::*FunctionPointer) (std::string);
You can just pass the name of the function without any decoration or any parentheses:
RegisterFunction(name, Run);
However, the following should also work:
RegisterFunction(name, &Run);
精彩评论