2011-08-31 17 views
5

तो मैं की तरह एक समारोह बनाना चाहते हैं:तर्क के साथ एक फ़ंक्शन कैसे बनाया जाए जो बढ़ावा :: बाइंड का परिणाम होगा?

void proxy_do_stuff(boost::bind return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 

और मुझे यह पसंद है कह सकते हैं:

proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument)); 

कैसे इस तरह के बात करना है?

उत्तर

3
#include <boost/bind.hpp> 

template<typename T> 
void proxy_do_stuff(T return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 

struct myclass 
{ 
    void myfunction(int, int) 
    { 
    } 
    void foo() 
    { 
     int my_function_argument_value = 3; 
     int etc_fun_argument= 5; 
     proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument)); 
    } 
}; 

int main() 
{ 
    myclass c; 
    c.foo(); 
    return 0; 
} 
+0

और इस तरह के फ़ंक्शन का उपयोग कैसे करें? क्या हमें इसे 'proxy_do_stuff (...) 'नहीं कहा जाना चाहिए? – Rella

+0

मैंने इसे जोड़ा कि इसका उपयोग कैसे किया जाए। ब्ला-ब्ला-ब्ला की कोई ज़रूरत नहीं है। –

4

रिटर्न प्रकार का बढ़ावा :: बाइंड प्रकार बूस्ट :: फ़ंक्शन है। नीचे देखें:

void proxy_do_stuff(boost::function<void()> return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 
+3

+1 रिटर्न प्रकार वास्तव में बढ़ावा देता है :: _ bi :: bind_t , boost :: _ bi :: list3 , boost :: _ bi :: value , boost :: _ bi :: value >> लेकिन आप इसे बढ़ावा देने के लिए परिवर्तित कर रहे हैं :: फ़ंक्शन