http://www.cppblog.com/gaimor/archive/2010/02/22/108236.html?opt=admin
我所知的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 #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 }
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比较好,只有一个头文件