Confusion with Pointers-c++(Objective C)
I am just going through an existing code and was confused how the function pointer gets called in the constructor of the base class as well as in the function called by the child class as this function pointer has not been assigned with the function's address to point to.
sMsgD.h: //Derived class of sMsg
#define DECLARE_MSG(ClassName,ClassID) \
/*lint -e(1511) we want to hide sMsg::NewMsg */ \
static ClassName* FromMsg(sMsg* pMsg) \
{ \
return dynamic_cast<ClassName*>(pMsg); \
} \
static sMsg* NewMsg() \
{ \
return new ClassName; \
} \
enum {ID = ClassID};
sMsgD.cpp: //Derived class of sMsg
void f()
{
stStruct* pf;
pf->pFunc();
}
sMsg.cpp: //Base class
class sMsg{
typedef sMsg* (*tMsg)(void);
typedef struct
{
int n;
NSString* s;
tMsg pFunc; //calls New for particular message
} stStruct;
sMsg();
};
sMsg::sMsg(int id,'tMsg pNewMsg',c开发者_StackOverflow社区onst char* szName,void* pt,size_t uSize)
{
//stmts;
}
This is the function pointer declaration and definition in C++. But i could not find how this function pointer is intialised with the address of the function NewMsg()
. And also sMsg is the base class where in its constructor the function pointer is passed.
How to implement the same in Objective C?
I think this is standard C syntax and not specific to C++, so it should work just the same in Objective-C, which is a strict superset of C (everything that works in C also works in Objective-C).
Objective C has a wonderful feature - blocks. I suspect they're more convenient and flexible than function pointers.
精彩评论