前篇:http://blog.csdn.net/pathuang68/archive/2009/04/23/4103627.aspx
在C++中,一個類執行個體化得到的結果就是一個對象。一個類包含成員變數和成員函數,其中成員變數又分為nonstatic成員變數和static成員變數;成員函數又可以分為nonstatic成員函數、static成員函數以及virtual成員函數。一個對象包含可能存在的vfptr以及它聲明的或基類繼承而來的nonstatic成員變數,static成員變數、static成員函數、nonstatic成員函數以及virtual函數均存在於對象之外。
VC2005中有一個非常重要的編譯選項:
對於查看類的對象的記憶體布局,微軟內部在VC2005中(要先進入Microsoft Visual Studio -> Visual Studio Tools -> Visual Studio 2005命令提示)提供了一個非常重要的編譯選項:/d1reportSingleClassLayout
比如,如果向查看檔案Polymorphism06.cpp中的類Child的對象在記憶體中的分布情況,先進入cmd命令視窗,改變目錄到Polymorphism06.cpp所在的目錄,然後鍵入如下命令:
cl Polymorphism06.cpp /d1reportSingleClassLayoutChild
斷行符號得到如下結果:
(注意:除上面關於記憶體布局的資訊外還有很多其他資訊)
有時我們會發現輸出中會出現vtordisp,它到底是是什麼意思呢,請看下面MSDN的解釋:
The vtordisp pragma is applicable only to code that uses virtual bases. If a derived class overrides a virtual function that it inherits from a virtual base class, and if a constructor or destructor for the derived class calls that function using a pointer to the virtual base class, the compiler may introduce additional hidden "vtordisp" fields into classes with virtual bases.
The vtordisp pragma affects the layout of classes that follow it. The /vd0 and /vd1 options specify the same behavior for complete modules. Specifying off suppresses the hidden vtordisp members. Specifying on, the default, enables them where they are necessary. Turn off vtordisp only if there is no possibility that the class's constructors and destructors call virtual functions on the object pointed to by the this pointer.
vtordisp is now deprecated and will be removed in a future release of Visual C++.
/vd編譯器選項會影響全域編譯模式。使用vtordisp編譯指示可以在基於類方式上開啟或禁止vtordisp域:
#pragma vtordisp(off)
class GetReal:virtual public{...};
#pragma vtordisp(on)
這是一個過時的東西,以後的VC編譯器也不會在支援了。
後篇:http://blog.csdn.net/pathuang68/archive/2009/04/24/4105626.aspx