【发布时间】:2017-09-28 11:52:55
【问题描述】:
我有一个基类、一个派生类和一个虚拟成员函数。我还有一个函数,它接受基类引用并对成员函数进行多态调用:
#include <iostream>
#include <functional>
class Base
{
public:
Base() {}
virtual int getnum() { return 1; }
};
class Derived : public Base
{
public:
Derived() {}
virtual int getnum() { return 2; }
};
int getnumref(Base& b) { return b.getnum(); }
int main()
{
Derived d;
Base& bref = d;
std::cout << getnumref(bref) << std::endl;
}
这里发生了后期绑定,输出为2。
但如果我现在将以下几行添加到main() 函数中,以便预先定义函数的参数,然后调用它:
std::function<int()> boundgetnumref = std::bind(getnumref, bref);
std::cout << boundgetnumref() << std::endl;
那么最后一行的输出是1,即这里发生了提前绑定,调用了基类的成员函数。
如果我使用指针,即
//...
int getnumptr(Base* b) { return b->getnum(); }
//...
int main()
{
Derived d;
Base* bptr = &d;
std::cout << getnumptr(bptr) << std::endl;
std::function<int()> boundgetnumptr = std::bind(getnumptr, bptr);
std::cout << boundgetnumptr() << std::endl;
}
那么两个cout 调用的输出都是2。
为什么当我将 pass-by-reference 与 std::bind 一起使用时会发生早期绑定,而不是其他情况?
【问题讨论】:
-
有点不相关:不要在现代代码中使用
std::bind,总是更喜欢 lambdas。见:youtu.be/zt7ThwVfap0?t=1754
标签: c++ pass-by-reference std-function stdbind