指向类成员函数的指针

时间:2014-08-26 07:54:26

标签: c++ class member-functions

我想将类成员函数地址存储到本地数据结构(表)

typedef struct
{
     unsigned int id; 
     void (TCLASS::*proc)();
} TSTRUCT;
class TCLASS{
  public:  
     void tfunct();  
     const  TSTRUCT t1 = { 1, &tfunct};
};

1 个答案:

答案 0 :(得分:0)

虽然你没有写一个问题,但假设你面临一堆编译错误。

见下文:

class TCLASS ; //forward declaration 

 struct TSTRUCT
{
     unsigned int id; 
     void (TCLASS::*proc)( );
    // // Use the TSTRUCT constructor
    TSTRUCT(int i, void (TCLASS::*fptr)( ) ): id(i), proc(fptr) 
    {
    }

} ;

class TCLASS{
  public:  
     void tfunct(); 
    TCLASS() : t1(1, &TCLASS::tfunct ) // initialize the const member
                    //~~~~~~~~~~~^ Use &TCLASS::tfunct instead of &tfunct
    {

    }
     const  TSTRUCT t1;
};
相关问题