VS2017中输出C++的指定类的布局

打开

语法
cl /d1reportSingleClassLayoutXXX yyy.cpp
注意那个是数字 1

用例
cl /d1reportSingleClassLayoutV3 vbptr.cpp

输出结果

vbptr.cpp
vbptr.cpp: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: 使用了 C++ 异常处理程序,但未启用展开语义。请指定 /EHsc
vbptr.cpp(31): warning C4311: “类型强制转换”: 从“int *”到“int”的指针截断
vbptr.cpp(32): warning C4311: “类型强制转换”: 从“int *”到“int”的指针截断

struct VB {
	virtual void f1() {};
	int vb;
};

class V1 :virtual public VB {
	virtual void f1() {};
	virtual void f2() {};
	int v1;
};
class V2 :virtual public VB {
	virtual void f1() {};
	virtual void f2() {};
	int v2;
};

class V3 :public V1, public V2 {
	virtual void f1() {};
	int v3;
};

class V3	size(72):
	+---
 0	| +--- (base class V1)
 0	| | {vfptr}
 8	| | {vbptr}
16	| | v1
  	| | <alignment member> (size=4)
	| +---
24	| +--- (base class V2)
24	| | {vfptr}
32	| | {vbptr}
40	| | v2
  	| | <alignment member> (size=4)
	| +---
48	| v3
  	| <alignment member> (size=4)
	+---
	+--- (virtual base VB)
56	| {vfptr}
64	| vb
  	| <alignment member> (size=4)
	+---

V3::$vftable@V1@:
	| &V3_meta
	|  0
 0	| &V1::f2 

V3::$vftable@V2@:
	| -24
 0	| &V2::f2 

V3::$vbtable@V1@:
 0	| -8
 1	| 48 (V3d(V1+8)VB)

V3::$vbtable@V2@:
 0	| -8
 1	| 24 (V3d(V2+8)VB)

V3::$vftable@VB@:
	| -56
 0	| &V3::f1 

V3::f1 this adjustor: 56
vbi:	   class  offset o.vbptr  o.vbte fVtorDisp
              VB      56       8       4 0
Microsoft (R) Incremental Linker Version 14.16.27045.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:vbptr.exe 
vbptr.obj 


用例2
vbptr.cpp
vbptr.cpp: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: 使用了 C++ 异常处理程序,但未启用展开语义。请指定 /EHsc

struct VB{
	virtual void f1() {};
	int vb;
};

class V1 :virtual public VB {
	virtual void f1() {};
	int v1;
};
class V2 :virtual public VB {
	virtual void f1() {};
	int v2;
};

class V3 :public V1, public V2 {
	virtual void f1() {};
	int v3;
};

class V3	size(56):
	+---
 0	| +--- (base class V1)
 0	| | {vbptr}
 8	| | v1
  	| | <alignment member> (size=4)
	| +---
16	| +--- (base class V2)
16	| | {vbptr}
24	| | v2
  	| | <alignment member> (size=4)
	| +---
32	| v3
  	| <alignment member> (size=4)
	+---
	+--- (virtual base VB)
40	| {vfptr}
48	| vb
  	| <alignment member> (size=4)
	+---

V3::$vbtable@V1@:
 0	| 0
 1	| 40 (V3d(V1+0)VB)

V3::$vbtable@V2@:
 0	| 0
 1	| 24 (V3d(V2+0)VB)

V3::$vftable@:
	| -40
 0	| &V3::f1 

V3::f1 this adjustor: 40
vbi:	   class  offset o.vbptr  o.vbte fVtorDisp
              VB      40       0       4 0
Microsoft (R) Incremental Linker Version 14.16.27045.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:vbptr.exe 
vbptr.obj 
********

posted @ 2021-01-11 14:49  iwetuan  阅读(388)  评论(0)    收藏  举报