· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Function Pointer In Class

C++ Ŭ·¡½º¿¡¼­ ÇÔ¼ö Æ÷ÀÎÅÍ »ç¿ëÇϱâ

  • Á¦¾àÁ¶°Ç
    1. Ŭ·¡½º ¸â¹öÀÎ ÇÔ¼ö Æ÷ÀÎÅÍ¿¡ ÇØ´ç Ŭ·¡½ºÀÇ ¸â¹öÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ÇÒ´çÇÏ´Â °ÍÀº °¡´ÉÇÏÁö¸¸ È£ÃâÀº ºÒ°¡´É.
    2. Ŭ·¡½º ¿ÜºÎ¿¡¼­ ÇØ´ç Ŭ·¡½º¿¡ ´ëÇÑ ÇÔ¼ö Æ÷ÀÎÅ͸¦ ¼±¾ðÇÏ°í, Ŭ·¡½º ¸â¹öÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ÇÒ´ç ¹× È£Ãâ °¡´É.
    3. Ŭ·¡½º ¸â¹ö º¯¼öÀÎ ÇÔ¼ö Æ÷ÀÎÅÍ¿¡ static ¸â¹öÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ÇÒ´ç ¹× È£Ãâ °¡´É.

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;
}


ID
Password
Join
Your mode of life will be changed for the better because of good news soon.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2006-06-11 20:30:38
Processing time 0.0044 sec