我所知的c++插槽系統由3個boost的,sigslot的,sigc++的
這里介紹sigc++的使用
最基本的使用方法:
1.回調函數為一般函數:
代碼如下:
1 #include <iostream>
2 #include <string>
3 #include <sigc++/sigc++.h>
4
5 //! 普通函數
6 void Print(const std::string& str)
7 {
8 std::cout << str;
9 }
10
11 int main()
12 {
13 //! 返回值void,參數const std::string&
14 sigc::signal<void, const std::string&> signal_print;
15 //! 鏈接函數
16 signal_print.connect( sigc::ptr_fun(&Print));
17 //! 發射信號
18 signal_print.emit("hello world\n");
19
20 system("pause");
21 return 0;
22 }
2.回調函數為成員函數
1 #include <iostream>
2 #include <string>
3 #include <sigc++/sigc++.h>
4
5 class Printer :public sigc::trackable
6 {
7 public:
8 void Work(){slot.emit("work\n");}
9 typedef sigc::signal<void, const std::string&> Slot;
10 Slot slot;
11 void Print(const std::string& str){std::cout<<str;}
12 };
13
14 int main()
15 {
16 Printer printer;
17 Printer::Slot::iterator iter = printer.slot.connect(sigc::mem_fun(&printer,&Printer::Print));
18 printer.Work();
19 iter->disconnect();
20 printer.Work();
21
22 system("pause");
23 return 0;
24 }
在sigc++中sigc::ptr_fun負責綁定一般函數
而sigc::men_fun負責綁定成員函數.
可以看到一般的信號插槽系統都具備以下幾個函數
a.插槽連接
b.插槽斷開
c.信號發射
當然有的插槽信號庫還提供其它一些函數
比如對信號設定優先級等等
這是簡單實用sigc++的例子
不過若論簡單性的話還是sigslot比較好,只有一個頭文件