المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Pointers to member functions which return pointers to themselves



C++ Programming
05-28-2011, 01:20 PM
#include #include /********************************************************************** The following class F compiles, runs, and does exactly what I WANT. What I DON'T LIKE about F is: 1.) Unnecessary enum 2.) Unnecessary static array of pointers 3.) High-maintenence coordination of the above two items I would LIKE to return the member function pointers directly. This looks rather like class G below (in which the self-referential nature of the necessary typedef introduces certain nervy concepts beyond my learning). **********************************************************************/ typedef enum {FUNK0=0, FUNK1, FUNK2, FUNK3, FUNK4, FUNK5, FUNK_N} F_ENUM; class F;typedef F_ENUM(F::* F_member_fn_ptr)(void); //Simple member function pointerclass F{public: F_ENUM F0(void) {printf("class F F0\n"); return(FUNK2);}; F_ENUM F1(void) {printf("class F F1\n"); return(FUNK3);}; F_ENUM F2(void) {printf("class F F2\n"); return(FUNK4);}; F_ENUM F3(void) {printf("class F F3\n"); return(FUNK5);}; F_ENUM F4(void) {printf("class F F4\n"); return(FUNK1);}; F_ENUM F5(void) {printf("class F F5\n"); return(FUNK_N);}; static F_member_fn_ptr FUNK[FUNK_N];}; F_member_fn_ptr F_FUNKS[FUNK_N]={&F::F0, &F::F1, &F::F2, &F::F3, &F::F4, &F::F5}; int main(){ F f; for(F_ENUM i=FUNK0; i<span class="code-keyword">