2007年6月的存档

R6025 - pure virtual function call

2007年06月04日 星期一

今天的一个程序出了 R6025 - pure virtual function call 错误,主要原因是在基类的构造函数中调用了纯虚函数。
1. 如果不是纯虚函数,没问题。
2. 如果构造函数直接调用纯虚函数,链接时会出错。只有通过一个其它成员函数转调一下。

下面是一个简化的例子:

class CBase
{
public:
    
CBase() { func2(); }
    
virtual void func() = 0;
    
void func2()
    
{
        
func();
    
}
};
 
class CDrived : public CBase
{
public:
    
CDrived() { }
    
virtual void func() { printf("hello"); }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    
CDrived * d = new CDrived();
 
    
return 0;
}

typeid

2007年06月03日 星期日
class Base
{
public:
    
void vvfunc() {}
};
 
class Derived : public Base
{
 
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    
Derived* pd = new Derived;
    
Base* pb = pd;
    
cout << typeid( pb ).name() << endl;   //prints "class Base *"
    
cout << typeid( pd ).name() << endl;   //prints "class Derived *"
 
    
try
    
{
//      Base * pp = NULL;
//      cout << typeid( *pp ).name() << endl;
        
cout << typeid( *pb ).name() << endl;   //prints "class Derived"
        
cout << typeid( *pd ).name() << endl;   //prints "class Derived"
    
}
    
catch (std::__non_rtti_object e)
    
{
        
cout << "__non_rtti_object" << endl;
    
}
    
catch (std::bad_typeid e)
    
{
        
cout << "bad_typeid exception" << endl;
    
}
    
delete pd;
 
    
return 0;
}

Visual C++ .NET下,如果/GR 未打开,这个程序仍然成功打印.

但如果 public: virtual void vvfunc(), 则 typeid( *pb ), typeid( *pd )的会错误,抛出__non_rtti_object异常。

如果上面注释的两句打开,则会跑出__bad_typeid异常。

__non_rtti_object继承自__bad_typeid.