vs开发人员命令查看C++类 data member 内存布局
生活随笔
收集整理的這篇文章主要介紹了
vs开发人员命令查看C++类 data member 内存布局
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++中類的數據成員在內存中時如何分布的,有繼承,虛擬繼承等情況下又是怎么分布的?在VS編譯器中可以查看。
源代碼如下:
#include<iostream> using namespace std;class CFurniture { public:CFurniture(){m_nPrice = 0;}~CFurniture(){printf("virtual ~CFurniture()\r\n");}virtual int GetPrice(){return m_nPrice;} protected:int m_nPrice; };class Csofa:virtual public CFurniture { public:Csofa(){m_nColor = 1;m_nColor = 2;}~Csofa(){printf("virtual ~Csofa()\r\n");}virtual int GetColor(){return m_nColor;}virtual int SitDown(){return printf("Sit Down and rest your legs\r\n");}protected:int m_nColor; };class CBed : virtual public CFurniture { public:CBed(){m_nPrice = 3;m_nLength = 4;m_nWidth = 5;}~CBed(){printf("virtual ~CBed()\r\n");}virtual int GetArea(){return m_nLength*m_nWidth;}virtual int Sleep(){return printf("go to sleep\r\n");}protected:int m_nLength;int m_nWidth; };class CSofaBed : public Csofa,public CBed { public:CSofaBed(){m_nHeight = 6;}~CSofaBed(){printf("virtual ~CSoftBed()\r\n");}virtual int SitDown(){return printf("Sir Down on the sofa bed\r\n");}virtual int Sleep(){return printf("go to sleep on the sofa bed\r\n");}virtual int GetHeight(){return m_nHeight;}protected:int m_nHeight; };void main(){CSofaBed SofaBed;CFurniture* pFunniture = &SofaBed;Csofa* pSofa = &SofaBed;CBed* pBed = &SofaBed; }1、打開 Visual Studio Tools 中的 VS2013 開發人員命令提示
2、切換到.cpp文件所在的目錄
3、輸入命令:cl -d1reportSingleClassLayout[類名] [文件名].cpp
4、也可以在編輯界面中,右擊[文件名].cpp->屬性–>配置屬性–>C/C+±->命令行:其他選項,添加:/d1reportSingleClassLayout[類名] [文件名].cpp
5、編譯,會在輸出欄中顯示上述信息。
5、用gcc的-fdump-class-hierarchy 參數把內存布局給打印出來
-fsyntax-only 只是檢查語法。
gcc -fdump-class-hierarchy -fsyntax-only CppTest.cpp
結果會在目錄下生成一個CppTest.cpp.t01.class的文本文件。
總結
以上是生活随笔為你收集整理的vs开发人员命令查看C++类 data member 内存布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python初识】列表与函数模块
- 下一篇: 【python初识】文件存储与异常