cocos2dx原始碼之 選擇器

2021-07-04 13:55:02 字數 1527 閱讀 3653

typedef 

void (ref::*sel_callfunc)();

typedef 

void (ref::*sel_callfuncn)(node*);

typedef 

void (ref::*sel_callfun***)(node*,void*);

typedef 

void (ref::*sel_callfunco)(ref*);

typedef 

void (ref::*sel_menuhandler)(ref*);

typedef 

void (ref::*sel_schedule)(float);

#define cc_callfunc_selector(_selector) static_cast(&_selector)

#define cc_callfuncn_selector(_selector) static_cast(&_selector)

#define cc_callfun***_selector(_selector) static_cast(&_selector)

#define cc_callfunco_selector(_selector) static_cast(&_selector)

#define cc_menu_selector(_selector) static_cast(&_selector)

#define cc_schedule_selector(_selector) static_cast(&_selector)

首先,定義無返回值無參乙個[函式]是這樣滴~

void func();

定義這個無返回值無參的[函式指標]呢,

void (*func)(); //用括號把*和func括起來,是為了防止和返回void指標並且無參的函式(即void* func())區分開來。

定義乙個無返回值無參的[函式指標]每次都要這麼寫,編寫c語言規則的人肯定覺得會很蛋疼,為了方便大眾,創造了乙個定義乙個[函式指標型別]的方法。

即: typedef 返回值 (命名空間::*函式指標型別名字)(引數列表)

typedef void (*func)(void)//定義乙個無返回值無參的指標型別,叫func,和int float一樣,它是乙個型別。

不要覺得這個typedef和以前看過的不一樣,不太順眼,這只是乙個固有用法而已,記住即可。不必糾結。

至於下面**擇器就比較好解釋了,

一般使用的時候都是這麼使用的,cc_schedule_selector(***node::func);

即 static_cast(&***node::func);

大家都知道,&取位址操作,返回值是乙個void*型別,即[無型別的指標]。

要想使用它必須把它強制轉換成能用的型別,

就是func型別,所以我們也可以寫乙個無返回值無參的選擇器

#define cc_noparam_noreturn_selector(_selector) static_cast(&_selector)

最後說上幾句,

cocos2dx學習筆記(選擇器)

選擇器類似於c 中類函式指標機制,用來建立函式指標,表示被指向的類方法 schedule selector selector 定時器 callfunc selector selector callfuncn selector selector callfun selector selector ca...

cocos2d x 原始碼剖析(2)

上次講到cocos2d x的main loop是下面這句 我們來看看這個函式的內部實現 return0 看看,我沒有欺騙大家吧。這個函式在設計的時候想要參照main函式返回乙個int值來表示執行結果,但是你知道的外部呼叫中完全沒有進行處理,略坑爹。來深入這個函式的內部 void startmainl...

cocos2d x 原始碼剖析(1)

原文出處 我認為在看這些文章的時候,最好有一些cocos2d x的經驗。起碼能新建乙個cocos2d x的hello world工程。而且這些文章並不是用來入門和教你如何使用cocos2d x的,我的目標是看完這些文章之後,寫乙個完整的2d引擎將沒有問題。而且能夠為cocos2d x查漏補缺,看看那...