【问题标题】:Is it possible to point a Typedef function pointer to a class member?是否可以将 Typedef 函数指针指向类成员?
【发布时间】:2011-12-13 16:52:56
【问题描述】:

我正在使用包含 DLL 的可执行文件。对于我的测试用例,我将代码组合成一个可执行文件。我正在使用 Visual Studio 2008 和 Boost 1.43。我试过研究这个,但没有找到任何明确的答案。感谢您的帮助。

在我的 main.h 中:

#include <string>

//These are normally defined in a seperate DLL
typedef std::string Typedef_func(const std::string & title);
void Register_My_Typedef(Typedef_func*);
//-------------------------------------------

class myClass
{
public:
    std::string func_one(const std::string & title);

    Typedef_func _test;

    void run();
};

在我的 main.cpp 中:

#include "main.h"
#include <boost/bind.hpp>

std::string workingFunc(const std::string & title)
{
    return "";
}

int main(int argc, char* argv[])
{
    myclass* example;
    example->run();

    Register_My_Typedef(&workingFunc);//This works.

    return 0;
}

void myClass::run()
{
    //I want to point a Typedef_func* in a DLL to call myclass::func_one
    Typedef_func* tf = boost::bind(&myClass::func_one, this, "test"); //This does not.

    Register_My_Typedef(tf);
}

std::string myClass::funcOne(const std::string & title)
{
    return "";
}

void Register_My_Typedef(Typedef_func* passedIn)
{
    //Points the pointer in the DLL to passedIn
}

当Register_My_Typedef 在一个不在类中的函数上调用时,DLL 逻辑可以正常工作,但是否可以从类中调用它?当我尝试编译这段代码时,它返回:

当我尝试在 Windows XP 中使用 VS2008 进行编译时,我得到:

错误 C2440:“正在初始化”:无法从 'boost::_bi::bind_t' 到 'Typedef_func (__cdecl *)' 与 [ R=std::字符串, F=升压::_mfi::mf1, L=升压::_bi::list2,升压::_bi::值> ]

没有可以执行此操作的用户定义转换运算符 转换,否则无法调用操作符。

【问题讨论】:

    标签: function-pointers typedef class-method


    【解决方案1】:

    答案是 Typedef 本身是静态成员函数的类成员,其行为与非静态不同,但最好阅读用户定义类 main 的类函数。

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2014-11-12
      • 2017-07-27
      • 2013-06-06
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多