여러 글 올림
Home
  • 분류 전체보기
    • 과제
      • 백준
      • 202X
    • 자격증
      • 정보처리기사
      • SQLD
    • 프로그래밍언어
      • CSSHTML
      • Python
      • C#
      • C++
Home
  • 분류 전체보기
    • 과제
      • 백준
      • 202X
    • 자격증
      • 정보처리기사
      • SQLD
    • 프로그래밍언어
      • CSSHTML
      • Python
      • C#
      • C++
블로그 내 검색

여러 글 올림

  • 프로그래밍언어/C++

    [C++ Builder] 이벤트 핸들러 사용해서 다른 클래스에 변수 넘겨주기

    2024. 5. 30.

    by. 쏘니빔

    //Hall.h
    
    typedef void __fastcall (__closure *TNameEvent)(String Name); // 함수 포인터 타입 정의
    
    class Hall
    {
        private:
        std::list<String> CookingList;
    
        public:
        TNameEvent OnCookingEvent; // 함수 포인터 멤버
        String MenuName;
        Hall();
        void AddCookingList(String MenuName);
        void __fastcall SetOnCookingEvent(TNameEvent handler) { OnCookingEvent = handler; }
        void FOnCookingEvent(String Name);
    };

     

    - typedef void __fastcall (__closure *TNameEvent)(String Name);

    함수 포인터를 선언하되 이벤트 변수를 만들기 위해 typedef로 선언

     

    - void AddCookingList(String MenuName);

    이벤트 발생시킬 함수

     

    - void __fastcall SetOnCookingEvent(TNameEvent handler) { OnCookingEvent = handler; }

    이벤트 핸들러 설정, 외부에서 이벤트 핸들러를 설정할 수 있도록해줌.

    다른 객체나 코드에서 원하는 함수 포인터를 설정할 수 있음.

    이벤트가 발생했을 때 호출할 함수를 동적으로 설정할 수 있음.

     

    - void FOnCookingEvent(String Name);

    문자열을 멤버변수에 저장하는 함수. 헤더파일에선 선언만 해주고 코드파일에서 저장할거임.


    //Hall.cpp
    
    #include "Hall.h"
    
    Hall::Hall()
    {}
    
    void Hall::FOnCookingEvent(String Name)
    {
        MenuName = Name;
    }
    
    
    void Hall::AddCookingList(String MenuName)
    {
        CookingList.push_back(MenuName);
        std::vector<std::vector<String>> Cooking = ini.ReadKeyValue(MenuName);
        String Name;
        String Time;
    
        try
        {
            for (size_t i = 0; i < Cooking.size(); ++i)
            {
                if (Cooking[i].size() >= 2)
                {
                    Name = Cooking[i][0]; // recipe name
                    Time = Cooking[i][1]; // recipe time
    
                    ...
    
                    // Name 다른 클래스에 전달 필요 (ing..에 쓰임)
                    if (OnCookingEvent)
                    {
                        OnCookingEvent(Name);
                    }
    
                    ...
                }
            }
        }
        __finally
        {
            delete Watch;
        }
    }

     

    if (OnCookingEvent)
    {

           OnCookingEvent(Name); //이벤트 발생시킴
    }

    이벤트 핸들러가 설정되어 있는지 확인하고, 설정되어 있다면 해당 이벤트 핸들러를 호출하여 이벤트를 발생시킴.

    OnCookingEvent는 함수 포인터로 특정 이벤트가 발생할 때 호출할 함수를 가리킴.


    //Main.h
    
    #ifndef RestaurantH
    #define RestaurantH
    #include <vcl.h>
    #include "Hall.h"
    
    class TForm1 : public TForm
    {
     ...
     private:    // User declarations
        void __fastcall LogControl(String Name); // 이벤트 핸들러 선언
     ...
    };

     

    헤더파일에 이벤트 핸들러 선언


    //Main.cpp
    
    #include "Hall.h"
    
    TForm1 *Form1;
    Hall hall;
    
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        ...
    
        // 이벤트 핸들러 등록
        hall.SetOnCookingEvent(this->LogControl);
    }
    
    //---------------------------------------------------------------------------
    void __fastcall TForm1::LogControl(String Name)
    {
        RichEdit1->Lines->Add(Name + " is being cooked...");
    }

     

    이벤트 핸들러 등록하고

    이벤트 발생되면 Name 받아옴

     

     

     

     

     

     

    참고 : http://www.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_qna&no=63350

    '프로그래밍언어 > C++' 카테고리의 다른 글

    [Visual C++] 헤더파일, 라이브러리 파일 사용  (0) 2024.08.29
    [C++] 타이머 동적 생성  (0) 2024.05.30
    [C++ Error] Restaurant.cpp(206): E2093 'operator*' not implemented in type '_STL::list<AnsiString,_STL::allocator<AnsiString> >' for arguments of the same type  (0) 2024.05.29
    [C++ Builder] 그리드에서 선택한 값을 String에 넣고 싶을 때, 편집 할 수 있게 바꾸고 싶을 때  (0) 2024.05.28
    [C++ Builder] TIniFile 클래스  (0) 2024.05.28

    댓글

    관련글

    • [Visual C++] 헤더파일, 라이브러리 파일 사용 2024.08.29
    • [C++] 타이머 동적 생성 2024.05.30
    • [C++ Error] Restaurant.cpp(206): E2093 'operator*' not implemented in type '_STL::list<AnsiString,_STL::allocator<AnsiString> >' for arguments of the same type 2024.05.29
    • [C++ Builder] 그리드에서 선택한 값을 String에 넣고 싶을 때, 편집 할 수 있게 바꾸고 싶을 때 2024.05.28
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

Today

Total

Powered by ⓒ Kakao Corp.

Designed by Nana
블로그 이미지
쏘니빔

티스토리툴바