【问题标题】:Passing any class member function as parameter将任何类成员函数作为参数传递
【发布时间】:2013-04-13 08:42:13
【问题描述】:

我正在尝试将类成员函数作为参数传递 当我使用以下代码时,这可以完美地工作

#include <stdio.h>

class CMother;
typedef int(CMother::*FuncPtr)(char* msg);

class CMother
{
protected:
    void SetFunctionPtr(FuncPtr ptr)
    {
        //get ptr here
    }
};

class CSon : public CMother
{
public:
    CSon()
    {
        SetFunctionPtr((FuncPtr)MyFunc);
    }
private:
    int MyFunc(char* msg)
    {
        printf(msg);
        return 0;
    }
};

int main()
{
    CSon son;
    return 0;
}

但是当我尝试使用模板概括 typedef 部分时,我得到了 fatal error C1001: INTERNAL COMPILER ERROR 产生这个错误的完整代码是

#include <stdio.h>

template<class T>
typedef int(T::*FuncPtr)(char* msg);

class CMother
{
protected:
    void SetFunctionPtr(FuncPtr ptr)
    {
        //get ptr here
    }
};

class CSon : public CMother
{
public:
    CSon()
    {
        SetFunctionPtr(MyFunc);
    }
private:
    int MyFunc(char* msg)
    {
        printf(msg);
        return 0;
    }
};

void mmm()
{
    CSon son;
}

谁能帮帮我。

【问题讨论】:

  • 我知道这不是你要问的,但是你为什么不能使用虚函数呢?

标签: c++ member-functions


【解决方案1】:

在 C++11 之前,C++ 没有模板类型定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2018-09-18
    相关资源
    最近更新 更多