升压::绑定&具有部分参数的 boost::function

发布于 2025-01-01 06:40:58 字数 594 浏览 4 评论 0原文

我向您发布了我想要做的事情的示例,以这种方式更容易解释

    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
     }

任何想法吗?

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
     }

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-01-08 06:40:58

跳过这个论点。

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

这仅绑定第一个参数。

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

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

Just skip that argument.

   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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文