2013-04-13 25 views
0

我想要當我使用如下因素代碼傳遞任何類的成員函數的參數

#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; 
} 

任何一個可以幫我這個請的完整代碼。

+1

我知道這是不是你問什麼,但爲什麼你不能使用虛擬函數? –

回答

1

C++沒有模板的typedef,直到C++ 11。

相關問題