提高::绑定和放大器;加强与部分ARGS ::功能 [英] boost::bind & boost::function with partial args

查看:158
本文介绍了提高::绑定和放大器;加强与部分ARGS ::功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我后你什么,我想要做一个例子,这是比较容易通过这种方式来解释

I post you an example of what I want to do, that is easier to explain in this way

    void myPrinter(const char* text, int number){            
            printf("\n%s %d\n", text, number);
        }

    int main() {

        char *someText="test";        

       boost::function<void(int my_number)> functionWithSavedArgs = boost::bind(&myPrinter, someText, ?????);

       //then I have to call my function with saved args and give to it only variable "number" like:
       int myBeautifulNumber = 2012;
       functionWithSavedArgs(myBeautifulNumber);
       // echo: test 2012
     }

任何想法?

推荐答案

跳过这样的说法。

   boost::function<void(int my_number)> functionWithSavedArgs
        = boost::bind(&myPrinter, someText);

这只是绑定第一个参数。

This binds only the first argument.

如果你想绑定只有第二个,你需要一个占位符:

If you wanted to bind only the second one, you would need a placeholder:

   boost::function<void(int my_number)> functionWithSavedArgs
         = boost::bind(&myPrinter, _1, someNumber);

这篇关于提高::绑定和放大器;加强与部分ARGS ::功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆