gpt4 book ai didi

c++ - boost::bind & boost::function with partial args

转载 作者:行者123 更新时间:2023-11-30 04:29:50 24 4
gpt4 key购买 nike

我给你发了一个我想做的例子,用这种方式更容易解释

    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);

这只绑定(bind)第一个参数。

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

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

关于c++ - boost::bind & boost::function with partial args,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com