2011-04-23 691 views
0
#define DECLARE_NEWMSG(ClassName,ClassID)     
static ClassName* FromMsg(ClassA* psg)    
{              
    return dynamic_cast<ClassName*>(psg);    
}              
static ClassA* newMsg()        
{              
    return new ClassName;        
}              
enum {ID = ClassID}; 

GET_EVENT = 0x41, 

typedef ClassA* (*pNewMsg)(void); //function pointer 

typedef struct 
{ 
    int   Id;  
    CString  Ascii;  
    pNewMsg MessageFunc; //calls new for the particular message 
} stStruct; 

ClassA 
{ 
//stmts; 
}; 

ClassA::ClassA(int Id,pNewMsg newMsg,const char* Ascii,void* Data,size_t DataSize) 
{ 
    initialises all the variables; 
} 

class GetEvent : public ClassA 
{ 
public: 
    DECLARE_NEWMSG(GetEvent,GET_EVENT); 
    GetEvent(); 
}; 


static const GetEvent cGetEvent; 
GetEvent::GetEvent():ClassA(ID, newMsg, NULL, NULL, 0) 
{ 
//no stmts inside; 
} 

无法理解GetEvent :: GetEvent():ClassA(ID,NewMsg,NULL,NULL,0)行。它们是否将派生值然后将其分配到基类的构造函数中。如果newMsg在调用基类构造函数时被定义为未定义,那么newMsg将保留为未定义状态。 如果采用相反的情况,则ID将保留为未定义状态。 。在C++中调用的基类和派生类构造函数

莫非不能够理解语句的执行,

static const GetEvent cGetEvent; 
GetEvent::GetEvent():ClassA(ID, newMsg, NULL, NULL, 0) 
{ 
//no stmts inside; 
} 

并且还函数指针,

typedef ClassA* (*pNewMsg)(void); 

#define DECLARE_NEWMSG(ClassName,ClassID)     
static ClassName* FromMsg(ClassA* psg)    
{              
    return dynamic_cast<ClassName*>(psg);    
}              
static ClassA* newMsg()        
{              
    return new ClassName;        
} 

回答

2
static const GetEvent cGetEvent; 
GetEvent::GetEvent():ClassA(ID, newMsg, NULL, NULL, 0) 
{ 
//no stmts inside; 
} 

该语法被称为initialization list,并且可以用于施工期间初始化类成员。构建派生类对象时,其父类也将被初始化(从层次结构的顶部开始)。在这种情况下,GetEvent构造函数指定如何构造对象(尽管读取代码,classA的实际类定义未指定此构造函数)。

typedef ClassA* (*pNewMsg)(void); 

这个函数指针的typedef指...

一个函数指针,我们可以称之为pNewMesg它没有参数(void),并返回一个指向ClassA对象。

在C++ FAQ中对函数指针typedef语法的稍微更好的解释:http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5

相关问题