C++ understanding cocos2d-x use of function pointers -
i experimenting around extending cocos2d-x ccmenuitem components , came across have not seen before in c++. helpful if elaborate on going on function pointer declarations
the base class cocos2d-x objects ccobject has following definition
class cc_dll ccobject : public cccopying { public: // code omitted }; // part in have question typedef void (ccobject::*sel_schedule)(float); typedef void (ccobject::*sel_callfunc)(); typedef void (ccobject::*sel_callfuncn)(ccnode*); typedef void (ccobject::*sel_callfuncnd)(ccnode*, void*); typedef void (ccobject::*sel_callfunco)(ccobject*); typedef void (ccobject::*sel_menuhandler)(ccobject*); typedef void (ccobject::*sel_eventhandler)(ccevent*); typedef int (ccobject::*sel_compare)(ccobject*); #define schedule_selector(_selector) (sel_schedule)(&_selector) #define callfunc_selector(_selector) (sel_callfunc)(&_selector) #define callfuncn_selector(_selector) (sel_callfuncn)(&_selector) #define callfuncnd_selector(_selector) (sel_callfuncnd)(&_selector) #define callfunco_selector(_selector) (sel_callfunco)(&_selector) #define menu_selector(_selector) (sel_menuhandler)(&_selector) #define event_selector(_selector) (sel_eventhandler)(&_selector) #define compare_selector(_selector) (sel_compare)(&_selector) so outside of ccobject class, within cocos2d namespace, there exists declaration function pointers , helper macros use them. correct in calling these declarations function pointers?
i understand typedef associating keyword type (see typedef function pointer?) , return type must void , function must have 1 mandatory argument of ccobject *. lost in understanding appropriate usage, scope, , how c++ treats passing function through function.
question 1
i not following how interpret scope of declared function pointer. in declaration, show function pointer being scoped ccobject class. how should interpret this? mean when assigned function belongs member function ccobject? confusing me defined outside of class body scoped ccobject.
typedef void (ccobject::*sel_menuhandler)(ccobject*); question 2
in cocos2d-x ccmenuitem class, has static factory method defined below
// how c++ treat this? function treated object here? static ccmenuitem* create(ccobject *rec, sel_menuhandler selector); ccmenuitem* ccmenuitem::create(ccobject *rec, sel_menuhandler selector) { ccmenuitem *pret = new ccmenuitem(); pret->initwithtarget(rec, selector); pret->autorelease(); return pret; } bool ccmenuitem::initwithtarget(ccobject *rec, sel_menuhandler selector) { setanchorpoint(ccp(0.5f, 0.5f)); m_plistener = rec; m_pfnselector = selector; m_benabled = true; m_bselected = false; return true; } // snippet ccmenuitem header protected: ccobject* m_plistener; sel_menuhandler m_pfnselector; // member variable stores pointer function? int m_nscripttaphandler; }; so typedef mean when pass function it, passing value pointer? how c++ handle if function not passed pointer. function treated object copy constructor?
i appreciate , advice. thanks
void (ccobject::*)(ccobject*) method pointer type (one type of pointer member), not ordinary function pointer. it's pointer can point instance method of ccobject class takes parameter of ccobject* type. type of class it's method of part of pointer type (denoted ccobject::), similar parameters (because underneath, pointer "current object" hidden parameter instance methods, this).
the typedef defines sel_menuhandler synonym method pointer type.
to use method pointer, need provide both instance act this, arguments, using syntax this:
ccobject* object; ccobject* anotherobject; sel_menuhandler methodpointer; (object->*methodpointer)(anotherobject); // or equivalently: ((*object).*methodpointer)(anotherobject); how c++ handle if function not passed pointer. function treated object copy constructor?
in c/c++, not possible have expression of "function type" or "method type". time try of "function type", automatically converted "pointer function" type.
Comments
Post a Comment