免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2090 | 回复: 1
打印 上一主题 下一主题

[C++] C++的虚表到底是怎么玩的?求指教 [复制链接]

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
1 [报告]
发表于 2013-11-26 16:57 |显示全部楼层

  1. $ g++ -fdump-class-hierarchy
复制代码
http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
-fdump-class-hierarchy (C++ only)
-fdump-class-hierarchy-options (C++ only)
    Dump a representation of each class's hierarchy and virtual function table layout to a file. The file name is made by appending .class to the source file name, and the file is created in the same directory as the output file. If the ‘-options’ form is used, options controls the details of the dump as described for the -fdump-tree options.

  1. $ cl /d1reportAllClassLayout
复制代码
http://connect.microsoft.com/Vis ... ject-layout-browser

...
In the mean time, you can use the undocumented (and unsupported) compiler switches /d1reportSingleClassLayout<class> and /d1reportAllClassLayout to get the information you want from your build. For more information on these switches, please read Andy's excellent blog post: http://blogs.msdn.com/vcblog/arc ... fixing-lnk2022.aspx

Peter-Michael Osera
Visual C++ Compiler Team

  1. $ clang -cc1 --help | grep layouts
  2.   -fdump-record-layouts   Dump record layout information
  3.   -fdump-vtable-layouts   Dump the layouts of all vtables that will be emitted in a translation unit
复制代码
建议将代码改成这样再去试:

  1. class ca { public: virtual ~ca() {} };
  2. class cb { public: virtual ~cb() {} };
  3. class cc : public ca, public cb { public: virtual ~cc() {} };
  4. class cd: public cc { public: virtual ~cd() {} };

  5. int main(int argc, char* argv[])
  6. {
  7.       switch (argc)
  8.       {
  9.       default: return 0;
  10.       case 1: return sizeof(ca);
  11.       case 2: return sizeof(cb);
  12.       case 3: return sizeof(cc);
  13.       case 4: return sizeof(cd);
  14.       }
  15. }
复制代码

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
2 [报告]
发表于 2013-11-26 17:44 |显示全部楼层
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带也不乱搞男女关系。
即使加进去过不了一周就会屏蔽群消息 —— 群里至少有3位能作证我这不是在乱说。
所以还是算了吧。啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP