1. 程式人生 > >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、編譯,會在輸出欄中顯示上述資訊。