내가 당신이라면이 작업을 수행하는 데 함수 포인터를 사용하지 않을 것입니다.)
Boost에는 signals이라는 멋진 라이브러리가 있습니다. 그것은 당신의 인생을 더 쉽게 만듭니다! 이 인쇄됩니다
#include <iostream>
#include <boost/bind.hpp>
#include <boost/signal.hpp>
using namespace std;
using namespace boost;
struct A
{ void A_action() { cout << "A::A_action();" << endl; } };
struct B
{ void B_action() { cout << "B::B_action();" << endl; } };
struct C
{ void C_action() { cout << "C::C_action();" << endl; } };
struct X
{
// Put all the functions you want to notify!
signal<void()> list_of_actions;
void do_something()
{
std::cout << "Hello I am X!" << endl;
list_of_actions(); // send notifications to all functions in the list!
}
};
int main()
{
X x;
A a;
B b;
C c;
x.list_of_actions.connect(bind(&A::A_action, a));
x.list_of_actions.connect(bind(&B::B_action, b));
x.list_of_actions.connect(bind(&C::C_action, c));
x.do_something();
}
: 그것이 작동하는 방법
다음
Hello I am X!
A::A_action();
B::B_action();
C::C_action();
인이 사용의 예입니다.
signal<void()> list_of_actions;
그런 다음 통화 할 기능/펑/호출 것들을 무엇을 적 그룹에 "연결"
첫째, 당신은 대의원을 보유하고 장소를 선언합니다.
x.list_of_actions.connect(bind(&A::A_action, a));
x.list_of_actions.connect(bind(&B::B_action, b));
x.list_of_actions.connect(bind(&C::C_action, c));
참고로 bind
을 사용했습니다. 따라서 list_of_actions에있는 함수 유형은 동일하지만 다른 유형의 클래스에 연결할 수 있습니다. 그래서 : 우리가 이전에 list_of actions
의 유형을 선언
bind(&A::A_action, a)
이 점은 입력 void()
의, 호출 가능한 일을 생성합니다. 물론이 멤버 함수를 적용 할 인스턴스를 두 번째 매개 변수로 지정하십시오.
멀티 스레드 작업을 수행하는 경우 자매 인 signals2을 사용하십시오.
희망이 있습니다.
시간을내어 예제를 추가해 주신 Arak에게 감사드립니다. 그게 내 수색을 끝내. 훌륭한! – Bach
멋진 답변입니다. – Ross