示例代码:VS2008 建一个 console 程序就可以看了。哪天尝试一下vs2012,或许编译就有错误了!!!
class A;
void fcn( A* );
class A
{
public:
virtual void f() = 0;
A() { fcn( this ); }
};
class B : A
{
void f() { }
};
void fcn( A* p )
{
p->f();
}
// The declaration below invokes class B’s constructor, which
// first calls class A’s constructor, which calls fcn. Then
// fcn calls A::f, which is a pure virtual function, and
// this causes the run-time error. B has not been constructed
// at this point, so the B::f cannot be called. You would not
// want it to be called because it could depend on something
// in B that has not been initialized yet.
B b;
(如上所述,b构造, 先构造 A(), 然后call fcn(this)然后 call A::f, 因为f 是 pure function,而这个时候,B 还没有构造完成,
所以就 报错误了,但这个应该在 编译器的时候检查出来。或许编译器也有bug!!!
可以把断点直接放在
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\purevirt.c 就能知道哪个function 出错了!
参考:
0