【发布时间】:2013-09-11 11:46:18
【问题描述】:
为什么bind 两个版本都可以毫无问题地编译和工作,即使我在每次调用中使用不同的参数类型?
- 版本 1 -> 参数 foo 与
- 版本 2 -> foo 的参数地址
我预计版本 1 会产生编译错误...
#include <iostream>
#include <functional>
using namespace std;
class Test
{
public:
bool doSomething(){ std::cout << "xxx"; return true;};
};
int main() {
Test foo;
// Version 1 using foo
std::function<bool(void)> testFct = std::bind(&Test::doSomething, foo);
// Version 2 using &foo
std::function<bool(void)> testFct2 = std::bind(&Test::doSomething, &foo);
testFct();
testFct2();
return 0;
}
【问题讨论】:
-
因为您可以在对象和指向对象的指针上调用成员函数?
-
重复回答我的问题