#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;
}
Could not understand the line GetEvent::GetEvent():ClassA( ID, NewMsg, NULL, NULL, 0 )..Are they passing the received values from the derived class constructor and then assigning it into the base class constructor..if so then newMsg will be left undefined as it gets defined while calling the base class constructor…
If taken in the reverse case then the ID will be left undefined…
Could not able to understand the execution of the statement,
static const GetEvent cGetEvent;
GetEvent::GetEvent():ClassA( ID, newMsg, NULL, NULL, 0 )
{
//no stmts inside;
}
and also the function pointer,
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;
}
This syntax is called an initialization list, and can be used to initialize class members during construction. When you construct a derived class object, its parent classes will also be initialized (starting at the top of the hierarchy). In this case, the
GetEventconstructor specifies how to construct theClassAobject (although reading your code, the actual class definition ofclassAdoesn’t specify this constructor).This function pointer typedef means…
A function pointer that we can call
pNewMesgwhich takes no arguments (void) and returns a pointer to aClassAobject.Slightly better explanation of the function pointer typedef syntax at C++ FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5