본문 바로가기

old_Coding/C/C++

[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