|

楼主 |
发表于 2016-9-14 09:49:33
|
显示全部楼层
感谢zuiwuchang 指出问题,收货颇多。 关于问题做点解说,如下代码是会调度 test1的 拷贝构造函数,不过 boost bind实现同样如此,
boost bind对于类对象的值传递方式会调度不止一次
- boost::bind(&test1::do_something, t1, boost::lambda::_1, boost::lambda::_2)(x, y);
复制代码 、
2、关于参数的问题,若用 boost::lambda::_1 则有限制,最多只能放3个参数,boost::lambda::_1、boost::lambda::_2、boost::lambda::_3,
因为boost::lambda 之定义了这么三个,
而boost bind可容纳的参数最多可以放9个之多!(其中一个是类对象,其余8个用来放置参数),列子见如下代码
- #include <iostream>
- #include <boost/function.hpp>
- #include <boost/bind.hpp>
- #include <boost/lambda/lambda.hpp>
- class test1
- {
- public:
- test1() {}
- test1(const test1& p) {} // 拷贝构造
- ~test1() {}
- void do_something(int& x_ , int& y_ , int& z_ , int& f_ , int& g_ , int& h_ ,
- int& i_ , int& j_)
- {
- x_ = 7;
- y_ = 7;
- z_ = 7;
- f_ = 7;
- g_ = 7;
- h_ = 7;
- i_ = 7;
- j_ = 7;
- }
- };
- int main()
- {
- test1 t1;
- int x = 0, y = 0, z = 0, f = 0, g = 0, h = 0, i = 0 , j = 0 , k = 0;
- // bind中第二个参数 若是指针,则在内部不会调用对应的拷贝构造函数;否则要调用多次拷贝构造函数
- //boost::bind(&test1::do_something, /*t1*/&t1, boost::lambda::_1, boost::lambda::_2)(x, y);
- boost::bind(&test1::do_something, &t1,
- _1, _2, _3, _4, _5, _6, _7, _8)(x , y , z , f , g , h , i , j);
- return 0;
- }
复制代码
不吝赐教 |
|