Function Pointer In Class
C++ Ŭ·¡½º¿¡¼ ÇÔ¼ö Æ÷ÀÎÅÍ »ç¿ëÇϱ⠶
1.1. Á¦¾àÁ¶°Ç1 ¶¿¹
class A
{ public: // Ŭ·¡½º A¿¡ ´ëÇÑ Æã¼Ç Æ÷ÀÎÅÍ void (A::*FuncPtr)(void); void Func1(void) { cout<<"Func1"<<endl; } void Func2(void) { cout<<"Func2"<<endl; } void SetFuncPointer(int num) { if ( num == 1 ) this->FuncPtr = this-£¾Func1; else this->FuncPtr = this-£¾Func2; } void RunFuncPointer() { this->FuncPtr(); // £¼--- ÄÄÆÄÀÏ ¿¡·¯ ¹ß»ý } }; 1.2. Á¦¾àÁ¶°Ç2 ¶¿¹
class A
{ public: void Func1(void) { cout<<"Func1"<<endl; } void Func2(void) { cout<<"Func2"<<endl; } }; int main() { A obj; // Ŭ·¡½º A¿¡ ´ëÇÑ Æã¼Ç Æ÷ÀÎÅÍ º¯¼ö Á¤ÀÇ void (A::*pf)(void); pf = obj.Func1; (obj.*pf)(); pf = obj.Func2; (obj.*pf)(); return 0; } 1.3. Á¦¾àÁ¶°Ç3 ¶¿¹
class A
{ private: void (*FuncPtr)(void); public: static void Func1(void) { cout<<"Func1"<<endl; } static void Func2(void) { cout<<"Func2"<<endl; } void SetFuncPointer(int num) { if ( num == 1 ) this->FuncPtr = this->Func1; else this->FuncPtr = this->Func2; } void RunFuncPointer() { this->FuncPtr(); } }; int main() { A obj; int num; cout<<"select function(1, 2) : "; cin>>num; obj.SetFuncPointer(num); obj.RunFuncPointer(); return 0; } |
The universe is laughing behind your back. |