更多信息如果您在boost::signals
庫望去,你會看到一個例子很不錯的,這是非常優雅:
假設你有4種功能,如:
void print_sum(float x, float y)
{
std::cout << "The sum is " << x+y << std::endl;
}
void print_product(float x, float y)
{
std::cout << "The product is " << x*y << std::endl;
}
void print_difference(float x, float y)
{
std::cout << "The difference is " << x-y << std::endl;
}
void print_quotient(float x, float y)
{
std::cout << "The quotient is " << x/y << std::endl;
}
然後,如果你想叫他們一個優雅的方式嘗試:
boost::signal<void (float, float)> sig;
sig.connect(&print_sum);
sig.connect(&print_product);
sig.connect(&print_difference);
sig.connect(&print_quotient);
sig(5, 3);
,輸出是:
The sum is 8
The product is 15
The difference is 2
The quotient is 1.66667
建議:爲代碼示例使用 '代碼示例' 格式。這是答案編輯器中的「零和」按鈕。 – jwfearn 2008-09-24 15:39:05