레이블이 Function Pointer인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Function Pointer인 게시물을 표시합니다. 모든 게시물 표시

2009년 12월 27일 일요일

[C++] Function Pointer(3) (Solution)



It's my solution file compressed by zip.




* With finishing *

Maybe, it would be last about the test code of the function pointer. Later, I'm not sure whether I'm going to write about the function pointer in my server model (because there are so many extra-informations to explain).




- ps.
This blog is for my English abilities, and I'm not good at communicating in English. If you see grammatic, syntax or logical errors, or if you can't understand clearly, PLEASE COMMENT ON IT. Your comments definitely help me, and I really appreciate this. :)

- Written by Gordon


[C++] Function Pointer(2) (in class & with STL)



* Introduction *
Here is my second experience. I've often used class in C++, and I finally made it.


* Function Pointer in class (code & result)*
#include <iostream>
class cTemp1
{
private :
    int m_X ;
    int m_Y ;
    void (cTemp1::*m_PtrF1)(void) ;    
    int  (cTemp1::*m_PtrF2)(int _x, int _y ) ;
public:
    cTemp1(void);
    ~cTemp1(void);
    void Func1(void) ;
    int     Func2(int _x, int _y) ;
    void Run() ;
};
cTemp1::cTemp1(void) : m_X (5), m_Y(3)
{
    m_PtrF1 = &cTemp1::Func1 ;
    m_PtrF2 = &cTemp1::Func2 ;
}
cTemp1::~cTemp1(void)
{
}
void cTemp1::Func1(void)
{
    std::cout << "cTemp1::Func1" << std::endl ;
}
int     cTemp1::Func2(int _x, int _y)
{
    std::cout << "cTemp1::Func2" << "  X = " << _x+m_X << "  Y = " << _y+m_Y << std::endl ;
    return (_x + m_X + _y + m_Y) ;
}
void cTemp1::Run()
{
    (this->*m_PtrF1)() ;
    int Result = (this->*m_PtrF2)(20, 10) ;
    std::cout << "Result = " << Result << std::endl ;
}
int main()
{
    cTemp1 Instance ;
    Instance.Run() ;
    return 0 ;
}






* Function Pointer in class with STL::map (code & result) *
#include <iostream>

class cTemp2
{
private :
    typedef void(cTemp2::*fPtr)(int _x) ;
    std::map <int, fPtr>        m_Map ;
public :
    cTemp2(void) ;
    ~cTemp2(void) ;
    void Func1(int _x) ;
    void Func2(int _x) ;
    void Func3(int _x) ;
    void Func4(int _x) ;
    void Func5(int _x) ;
    void Func6(int _x) ;
    void Func7(int _x) ;
    void Func8(int _x) ;
    void Func9(int _x) ;
    void Func10(int _x) ;
    void Run() ;
};
cTemp2::cTemp2(void)
{
    // Insert to map
    m_Map[1] = &cTemp2::Func1 ;
    m_Map[2] = &cTemp2::Func2 ;
    m_Map[3] = &cTemp2::Func3 ;
    m_Map[4] = &cTemp2::Func4 ;
    m_Map[5] = &cTemp2::Func5 ;
    m_Map[6] = &cTemp2::Func6 ;
    m_Map[7] = &cTemp2::Func7 ;
    m_Map[8] = &cTemp2::Func8 ;
    m_Map[9] = &cTemp2::Func9 ;
    m_Map[10] = &cTemp2::Func10 ;

}
cTemp2::~cTemp2(void)
{
    // destroy map
    m_Map.clear() ;
}

void cTemp2::Func1(int _x) {std::cout << "cTemp2::Func1() is called with " << _x << std::endl ;}
void cTemp2::Func2(int _x) {std::cout << "cTemp2::Func2() is called with " << _x << std::endl ;}
void cTemp2::Func3(int _x) {std::cout << "cTemp2::Func3() is called with " << _x << std::endl ;}
void cTemp2::Func4(int _x) {std::cout << "cTemp2::Func4() is called with " << _x << std::endl ;}
void cTemp2::Func5(int _x) {std::cout << "cTemp2::Func5() is called with " << _x << std::endl ;}
void cTemp2::Func6(int _x) {std::cout << "cTemp2::Func6() is called with " << _x << std::endl ;}
void cTemp2::Func7(int _x) {std::cout << "cTemp2::Func7() is called with " << _x << std::endl ;}
void cTemp2::Func8(int _x) {std::cout << "cTemp2::Func8() is called with " << _x << std::endl ;}
void cTemp2::Func9(int _x) {std::cout << "cTemp2::Func9() is called with " << _x << std::endl ;}
void cTemp2::Func10(int _x) {std::cout << "cTemp2::Func10() is called with " << _x << std::endl ;}

void cTemp2::Run()
{
    int choice ;
    int count = 0 ;
    std::cout << "Enter a number between 1 and 10, 0 to end: " ;
    std::cin >> choice ;
    while (choice >= 1 && choice < 11) {
        std::map<int, fPtr>::iterator MapIt ;
        MapIt = m_Map.find (choice) ;
        if (MapIt->second != NULL) {
            (this->*(MapIt->second))(count) ;
        }
        count ++ ;
        std::cout << "Enter a number between 1 and 10, 0 to end: " ;
        std::cin >> choice ;
    }
    std::cout << "cTemp2::Run() end" << std::endl ;
}
int main()
{
   cTemp2 Instance2 ;
   Instance2.Run() ;
   return 0 ;
}







- ps.
This blog is for my English abilities, and I'm not good at communicating in English. If you see grammatic, syntax or logical errors, or if you can't understand clearly, PLEASE COMMENT ON IT. Your comments definitely help me, and I really appreciate this. :)

- Written by Gordon


[C++] Function Pointer(1) (Global function & using array)



* Introduction *
- A week ago, to parse network packets I used to do 'if' for all case. During optimizations, I turned from 'if' to 'function pointer with map'. I'm going to write about the function pointer as known as possible.


* Basic usage (code) *
#include <iostream>
void     Function1() ;
int        Function2(int _x, int _y) ;
int main()
{
    //////////////////////////////////////////////////////////////////////////
    // Test inside Main function
        // for void Function1() ;
    void (*PtrFunc1)() ;
    PtrFunc1 = Function1 ;
    PtrFunc1() ;
        // for int Function2(int, int) ;
    int  (*PtrFunc2)(int, int) ;
    PtrFunc2 = Function2 ;
    int result = PtrFunc2(20, 10) ;
    std::cout << "Result = " << result << std::endl ;
    return 0 ;
}
void Function1()
{
    std::cout << "Function1" << std::endl ;
}
int  Function2(int _x, int _y)
{
    std::cout << "Function2" << "  X = " << _x << "  Y = " << _y << std::endl ;
    return (_x + _y) ;
}






* Using with array *
#include <iostream>
void    ArrayFunction1(int _x) ;
void    ArrayFunction2(int _x) ;
void    ArrayFunction3(int _x) ;
int main()
{
    int choice ;
    void (*f[3])(int) = {ArrayFunction1, ArrayFunction2, ArrayFunction3} ;
    std::cout << "Enter a number between 0 and 2, 3 to end: " ;
    std::cin >> choice ;
    while (choice >= 0 && choice < 3) {
        (*f[choice])(choice) ;
        std::cout << "Enter a number between 0 and 2, 3 to end: " ;
        std::cin >> choice ;
    }
    return 0 ;
}
void ArrayFunction1( int _x )
{
    std::cout << "ArrayFunction1: _x = " << _x << std::endl ;
}
void ArrayFunction2( int _x )
{
    std::cout << "ArrayFunction2: _x = " << _x << std::endl ;
}
void ArrayFunction3( int _x )
{
    std::cout << "ArrayFunction3: _x = " << _x << std::endl ;
}







Reference : How To C++ Program 4th edition



- ps.
This blog is for my English abilities, and I'm not good at communicating in English. If you see grammatic, syntax or logical errors, or if you can't understand clearly, PLEASE COMMENT ON IT. Your comments definitely help me, and I really appreciate this. :)

- Written by Gordon