Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用C++;使用函数指针的函数_C++_C_Mfc - Fatal编程技术网

C++ 调用C++;使用函数指针的函数

C++ 调用C++;使用函数指针的函数,c++,c,mfc,C++,C,Mfc,我有一个函数,它接收一个函数指针和一个XML字符串,该字符串包含函数的定义和参数: void CallFunction( void * pFuncion, std::string xmlFuncionDef) { } 在上述函数中,xmlFunctionDef包含pFunction所指函数的定义。例如,number参数、每个参数的类型和参数: <Function ParamCount="3" ReturnType="int"> <Params> <P

我有一个函数,它接收一个函数指针和一个XML字符串,该字符串包含函数的定义和参数:

void CallFunction( void * pFuncion, std::string xmlFuncionDef)
{

}
在上述函数中,
xmlFunctionDef
包含
pFunction
所指函数的定义。例如,number参数、每个参数的类型和参数:

<Function ParamCount="3" ReturnType="int">
  <Params>
    <Param type="int" DefaultValue="None" PassBy="Value"/>
    <Param type="double" DefaultValue="None" PassBy="Referenc"/>
    <Param type="char*" DefaultValue="None" PassBy="Pointer"/>
  </Params>

  <Arguments>
   <Arg Value="3" />
   <Arg Value="29.5" />
   <Arg Value="Hello World" />
  </Arguments>
</Function>

现在我如何调用这个函数?我应该为此使用
\u asm


非常感谢您的帮助。

据我所知,函数指针不能有灵活的参数-您必须准确地告诉函数将接收哪种类型的参数。在您的示例中:

void (*pFunc)(int,double,char*)
当然,您可以使用void*作为唯一参数,然后处理这种可变性 内部:

void (*pFunc)(void*) 

但是,我相信这将是一个混乱的邀请。

最好的方法是将注册函数的代码位设置为其参数和返回类型上的模板。然后,您可以将其分离并返回一个lambda,它知道如何解析XML并调用函数。真正的hacky概念证明[它不处理varargs(硬编码为2),使用字符串而不是xml,假设类型是默认可构造的,不处理ref或move,并将大量内容留给读者作为练习]是:

#include <string>
#include <functional>
#include <sstream>
#include <iostream>
#include <stdexcept>

template<class FIRST, class SECOND, class RETURN_TYPE>
std::function<RETURN_TYPE(std::string const&)> RegisterFunction(
    RETURN_TYPE(func)(FIRST f, SECOND s))
{
    auto copy = *func;
    return[copy](std::string const& str)->RETURN_TYPE
    {
        std::istringstream inStream(str);
        FIRST f;
        inStream >> f;
        if(inStream.fail())
        {
            throw std::runtime_error("Couldn't parse param one");
        }
        SECOND s;
        inStream >> s;
        if(inStream.fail())
        {
            throw std::runtime_error("Couldn't parse param two");
        }
        // can check if inStream is eof here for consistency
        return copy(f, s);
    };
}

std::string MyFunc(float f, std::string s)
{
    std::ostringstream os;
    os << "MyFunc called with float " << f << " and string " + s;
    return os.str();
}


int main()
{
    auto wrapper = RegisterFunction(MyFunc);

    // Now try to call it
    std::cout << wrapper("42.0 HelloWorld") << std::endl;

    // Try to call it with an invalid string
    try
    {
        std::cout << wrapper("This isn't the number you are looking for");
    }
    catch(...)
    {
        std::cout << "Failed to call function" << std::endl;
    }
}

你是说
void调用函数(void(*pFuncion)(void),std::string xmlFuncionDef)
?你问的是“如何调用该函数”。但第一部分已经不清楚了:如何接收指向该函数的指针
void*pFuncion
是void类型的指针,它不是指向函数的指针。如果代码应在特定体系结构上运行,则可以使用内联汇编并将参数推送到堆栈上,然后调用函数。但是,您的函数负责以正确的顺序从堆栈中获取参数。不管怎样,你的问题没有明确的定义。更具体一点,并展示一些示例。看起来您正试图发明COM、SOAP web服务或类似的体系结构。这是一项相当大的任务。
MyFunc called with float 42 and string HelloWorld
Failed to call function