是否可以将Typedef函数指针指向类成员?

时间:2011-12-13 16:52:56

标签: function-pointers typedef class-method

我正在使用包含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逻辑工作正常,但是可以从类中调用它吗?当我尝试编译此代码时,它返回:

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

  

错误C2440:'初始化':无法转换   'boost :: _ bi :: bind_t'改为'Typedef_func(__ cdecl *)'   [       R =的std :: string,       F =提振:: _ MFI :: MF1,       L =升压:: _双向:: list2中,升压:: _双向::值GT;   ]

     

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

1 个答案:

答案 0 :(得分:-1)

答案是Typedef本身是静态成员函数的类成员&amp;行为与非静态不同,但最好是读取用户定义的类main的类功能。

相关问题