用MSVC創建一個QT 項目.似乎是自動設置成最低級別,WARNNING 1 ! 記住一定要自己設置回第三級別! 否則,無數C++中會導致萬劫不復的錯誤會被編譯器放過 。舉個例子:
===================
A.h
===================
class A
{
public :
A()
{
m_buf = new char[1024*1024*1024];//en ,... 1G ...just see see
};
virtual ~A()
{
printf("Oh A destroyed ");
delete []m_buf;
};
private:
char* m_buf;
};
====================
C.h
====================
class A;
class C
{
public:
C():m_a(0)
{
};
virtula ~C()
{
if (m_a)
delete m_a;
}
private:
A* m_a;
};
====================
D.h
====================
#include "C.h"
#include "a.h"
class D: public C
{
public :
D()
{
m_a = new A;
};
~D()
{
printf("oh D destroyed ");
};
}
===================
main.cpp
===================
#include "D.h"
int main()
{
C* d = new D;
delete d;
}
OK, 下面,不要編譯,把VC 的警告級別甚至成 /W1 。 然後,編譯順利通暢。
假象的運行結果是 經過~D()後,經過~C(), 接著m_a被銷毀。
真正結果呢。~A()根本沒有被執行過. 智者會痛罵, W3 明明告訴你 main.cpp根本沒找到~A()定義....恩恩, 說完了...