[英]Binding Virtual Functions in Parent Ctor
给出以下代码:
class foo
{
public:
foo() : _myFunc( bind( &foo::testCall, this ) ){}
virtual void testCall(){ cout << "foo" << endl; }
void call(){ _myFunc(); }
private:
function< void() > _myFunc;
};
class bar: public foo
{
public:
virtual void testCall(){ cout << "bar" << endl; }
};
void main()
{
bar test;
test.call();
}
为什么打印“条”。 我读了此问题 ,并以为会打印“ foo”。
您没有在绑定到成员变量的构造函数中调用虚拟函数,而是在后者调用该变量(在这种情况下,使用了动态分派)。
调用代码应为: test.call();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.