- 论坛徽章:
- 0
|
转自:http://www.allaboutprogram.com/about2074.html
boost::bind的文档,在http://boost.allaboutprogram.com/libs/bind/bind.html
- #include <iostream>;
- #include <boost/bind.hpp>;
- #include <boost/bind/apply.hpp>;
- using namespace std;
- int add( int i, int j )
- {
- return i + j;
- }
- int add_and_subtract( int i, int j, int k )
- {
- return i + j - k;
- }
- int pass( int& i )
- {
- i = 1;
- return i;
- }
- class functor
- {
- public:
- int operator()( int i, int j )
- {
- return i + j;
- }
- int operator()( int i, int j, int k )
- {
- return i + j - k;
- }
- };
- class person
- {
- public:
- person( const char* s, const char* n ): site( s ), name( n )
- {}
- const char* site;
- const char* name;
- const char* get_name() const
- {
- return name;
- }
- };
- int main()
- {
- cout << boost::bind( add, 5, 5 )( ) << endl;
- int i = 10;
- cout << boost::bind( add, _1, 5 )( i ) << endl;
- cout << boost::bind( add, _1, _1 )( i ) << endl;
- int j = 20;
- cout << boost::bind( add_and_subtract, _1, _1, _2 )( i, j ) << endl;
- cout << boost::bind( add_and_subtract, _1, _1, _2 )( i, j, j, j, j, j, j, j, j ) << endl;
- cout << boost::bind( pass, j )( ) << endl;
- cout << j << endl;
- cout << boost::bind( pass, boost::ref(j) )( ) << endl;
- cout << j << endl;
- cout << boost::bind<int>;( functor(), _1, _1 )( i ) << endl;
- cout << boost::bind<int>;( functor(), _1, _1, _1 )( i ) << endl;
- cout << boost::bind( boost::type<int>;(), functor(), _1, _1, _1 )( i ) << endl;
- person pora( "http://www.allaboutprogram.com/", "pora^-^" );
- cout << boost::bind<const char*>;( person::get_name, pora )() <<endl;
- cout << boost::bind<const char*>;( person::get_name, &pora )() <<endl;
- cout << boost::bind<const char*>;( &person::get_name, pora )() <<endl;
- cout << boost::bind( &person::site, pora )() <<endl;
- cout << boost::bind( add, _1, boost::bind( add_and_subtract, _1, _1, _1 ) )( i ) <<endl;
- // cout << boost::bind<int>;( _1, 5, 5 )( add ) << endl;
- cout << boost::bind( boost::apply<int>;(), _1, 5, 5 )( add ) << endl;
- cout << boost::bind( boost::type<int>;(), add, 5, 5 )( ) << endl;
- return 0;
- }
复制代码 |
|