C++ 함수 포인터 (Function Pointer)

2021. 3. 12. 17:30·C++/Syntax

함수 포인터 (Function Pointer)

기본 예제

배열에서 짝수, 홀수를 각각 출력하는 예제

  • 간단한 버전

      #include <iostream>
      #include <array>
    
      using namespace std;
    
      void    printNumbers(const array<int, 10>& arr, \
          bool print_even)
      {
          for (auto e : arr)
              if ((print_even && e % 2 == 0) || \
                  (!print_even && e % 2 == 1))
                  cout << e;
          cout << endl;
      }
    
      int     main()
      {
          array<int, 10> arr{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
          printNumbers(arr, true);
          printNumbers(arr, false);
      }
    
      /* stdout stderr
      02468
      13579
      */

  • 함수를 나누고 함수 포인터로 전달

    #include <iostream>
    #include <array>
    
    using namespace std;
    
    bool    isEven(const int& number)
    {
      if (number % 2 == 0)
        return true;
      else
        return false;
    }
    
    bool    isOdd(const int& number)
    {
      if (number % 2 == 1)
        return true;
      else
        return false;
    }
    
    void    printNumbers(const array<int, 10>& arr, \
      bool (*check_fcn)(const int&) = isEven)
    {
      for (auto e : arr)
        if (check_fcn(e))
          cout << e;
      cout << endl;
    }
    
    int     main()
    {
      array<int, 10> arr{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      printNumbers(arr);
      printNumbers(arr, isOdd);
    }
    
    /* stdout stderr
    02468
    13579
    */

  • typedef로 정리

    #include <iostream>
    #include <array>
    
    using namespace std;
    
    bool    isEven(const int& number)
    {
      if (number % 2 == 0)
        return true;
      else
        return false;
    }
    
    bool    isOdd(const int& number)
    {
      if (number % 2 == 1)
        return true;
      else
        return false;
    }
    
    typedef bool (*check_fcn_t)(const int&);
    //using check_fcn_t = bool (*)(const int&);
    
    void    printNumbers(const array<int, 10>& arr, \
      check_fcn_t check_fcn = isEven)
    {
      for (auto e : arr)
        if (check_fcn(e))
          cout << e;
      cout << endl;
    }
    
    int     main()
    {
      array<int, 10> arr{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      printNumbers(arr);
      printNumbers(arr, isOdd);
    }
    
    /* stdout stderr
    02468
    13579
    */

  • C++11의 <functional> 라이브러리 사용

    #include <iostream>
    #include <array>
    #include <functional>
    
    using namespace std;
    
    bool    isEven(const int& number)
    {
      if (number % 2 == 0)
        return true;
      else
        return false;
    }
    
    bool    isOdd(const int& number)
    {
      if (number % 2 == 1)
        return true;
      else
        return false;
    }
    
    void    printNumbers(const array<int, 10>& arr, \
      std::function<bool(const int&)> check_fcn)
    {
      for (auto e : arr)
        if (check_fcn(e))
          cout << e;
      cout << endl;
    }
    
    int     main()
    {
      array<int, 10> arr{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
      std::function<bool(const int&)> fcnptr = isEven;
    
      printNumbers(arr, fcnptr);
      fcnptr = isOdd;
      printNumbers(arr, fcnptr);
    }
    
    /* stdout stderr
    02468
    13579
    */
저작자표시 (새창열림)

'C++ > Syntax' 카테고리의 다른 글

C++ 깊은 복사(Deep Copy)  (0) 2021.03.15
C++ 얕은 복사(Shallow Copy)  (0) 2021.03.15
C++ 인라인 함수 (Inline Function)  (0) 2021.03.12
C++ 레퍼런스 (Reference, 참조)  (0) 2021.03.12
C++ Stack Size  (0) 2021.03.11
'C++/Syntax' 카테고리의 다른 글
  • C++ 깊은 복사(Deep Copy)
  • C++ 얕은 복사(Shallow Copy)
  • C++ 인라인 함수 (Inline Function)
  • C++ 레퍼런스 (Reference, 참조)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    java
    시스템 복구
    vscode
    windows
    mspaint
    EXCLUDE
    dism
    스프링 프레임워크 핵심 기술
    Solaris 10
    spring
    Workspace
    윈도우
    로지텍 마우스 제스처
    unix
    윈도우 명령어
    그림판
    백기선
    citrix workspace
    알림
    KakaoTalk
    SunOS 5.1
    MacOS
    스프링
    Windows 11
    logi options
    SFC
    맥북 카카오톡 알림 안뜸
    제외
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 함수 포인터 (Function Pointer)
상단으로

티스토리툴바