【发布时间】:2013-04-15 13:14:55
【问题描述】:
我有这个代码:
#include <iostream>
#include <functional>
struct Foo
{
int get(int n) { return 5+n; }
};
int main()
{
Foo foo;
auto L = std::bind(&Foo::get, &foo, 3);
std::cout << L() << std::endl;
return 0;
}
好像是这样的:
auto L = std::bind(&Foo::get, &foo, 3);
相当于:
auto L = std::bind(&Foo::get, foo, 3);
为什么?
【问题讨论】:
-
不是。一个绑定一个指针,另一个绑定一个copy。
-
对于它的价值,您还可以传递一个智能指针(实现
operator->以返回foo*的任何类型)作为第二个参数。试试std::shared_ptr。 -
重复:stackoverflow.com/questions/15264003/… 虽然我喜欢这两个答案...