R6025调用纯虚函数错误

   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();//here, call the version of class A.
   }



int main(int argc, char **argv)
{
	/*construct object A first, in constructor of class A, it call virtual function f(); theoretically it should call 
        the version of class B, because it is object B. But at this time, object b has not been fully constructed yet. 
        That means the virtual function table has not been built up yet. so it actually calls the version of class A, 
        which cause the error of R6025*/
	B b;
	return 0;
}


所以结论是在构造函数和析构函数中不要调用虚函数,实际项目中case会比较复杂,往往没有这么容易看出来

你可能感兴趣的:(R6025调用纯虚函数错误)