【整理】查看C++对象内存布局

     在C++中,一个类实例化得到的结果就是一个对象。一个类包含成员变量和成员函数,成员变量分为non-static和static,成员函数分为non-static成员函数、static成员函数以及virtual成员函数一个对象包含可能存在的vfptr以及它声明的或基类继承而来的non-static成员变量,static成员变量、static成员函数、non-static成员函数以及virtual函数均存在于对象之外

    VC2005中有一个非常重要的编译选项:对于查看类的对象的内存布局,MS在VC2005中(要先进入Microsoft Visual Studio -> Visual Studio Tools -> Visual Studio 2005命令提示)提供了一个非常重要的编译选项:/d1reportSingleClassLayout

    如果想向查看文件Polymorphism06.cpp中的类Child的对象在内存中的分布情况,先进入cmd命令窗口,改变目录到Polymorphism06.cpp所在的目录,然后键入如下命令:

        cl Polymorphism06.cpp /d1reportSingleClassLayoutChild

    回车得到如下结果: (注意:除关于内存布局的信息外还有很多其他信息)34a63244db4292efb3b7dc04.jpg   

    有时我们会发现输出中会出现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编译器也不会在支持了。

转载于:https://www.cnblogs.com/sopc-mc/archive/2011/10/12/2208466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值