免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
3
摩羯座
日期:2013-11-12 20:06:19午马
日期:2013-11-27 16:35:55双鱼座
日期:2014-04-04 19:02:30
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-26 09:35 |只看该作者 |倒序浏览
  1. /*
  2. * =====================================================================================
  3. *
  4. *       filename:  test.cpp
  5. *
  6. *    description:  
  7. *
  8. *        version:  1.0
  9. *        created:  11/25/2013 08:17:32 pm
  10. *       revision:  none
  11. *       compiler:  gcc
  12. *
  13. *         author:  alan wang (), alan.3976@gmail.com
  14. *   organization:  
  15. *
  16. * =====================================================================================
  17. */
  18. #include <iostream>
  19. using namespace std;

  20. class ca {

  21.     public:
  22.         virtual ~ca() {
  23.             cout << "deconstructor in ca !" << endl;
  24.         }
  25. };

  26. class cb {

  27.     public:
  28.        virtual ~cb() {
  29.             cout << "deconstructor in cb !" << endl;
  30.        }
  31. };

  32. class cc : public ca, public cb {
  33.    
  34.     public:
  35.         virtual ~cc() {
  36.             cout << "deconstructor in cc !" << endl;
  37.         }
  38. };


  39. class cd: public cc {
  40.     public:
  41.         virtual ~cd() {
  42.             cout << "deconstructor in cd !" << endl;
  43.         }

  44. };

  45. int main() {

  46.     cd cd;
  47.     cc cc;
  48.     cout << "sizeof cc is " << sizeof(cc) << endl;
  49.     cout << "sizeof cd is " << sizeof(cd) << endl;
  50.     return 0;
  51. }
复制代码
我的机器是64位的,cc 和 cd的sizeof打印出来都是16,我理解的是 cc中包含了两个(CA & CB)虚表的指针,但是CD呢? CD为什么也是16,CC中的虚表指针呢?



糊涂了,想不明白了,谁来指导一下。

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
2 [报告]
发表于 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. }
复制代码

论坛徽章:
4
摩羯座
日期:2013-11-24 21:55:03天秤座
日期:2013-11-25 20:54:14酉鸡
日期:2013-11-26 22:08:11天蝎座
日期:2013-12-07 07:32:45
3 [报告]
发表于 2013-11-26 17:05 |只看该作者
OwnWaterloo 发表于 2013-11-26 16:57
建议将代码改成这样再去试:

我们群加人,你要进来吗?

305369284

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

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

论坛徽章:
4
摩羯座
日期:2013-11-24 21:55:03天秤座
日期:2013-11-25 20:54:14酉鸡
日期:2013-11-26 22:08:11天蝎座
日期:2013-12-07 07:32:45
5 [报告]
发表于 2013-11-26 17:48 |只看该作者
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带 ...

之前不是听说你在做网站?然后又在做嵌入式

论坛徽章:
4
摩羯座
日期:2013-11-24 21:55:03天秤座
日期:2013-11-25 20:54:14酉鸡
日期:2013-11-26 22:08:11天蝎座
日期:2013-12-07 07:32:45
6 [报告]
发表于 2013-11-26 17:52 |只看该作者
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带 ...

屏蔽群消息没关系,我们群基本不说话

论坛徽章:
4
摩羯座
日期:2013-11-24 21:55:03天秤座
日期:2013-11-25 20:54:14酉鸡
日期:2013-11-26 22:08:11天蝎座
日期:2013-12-07 07:32:45
7 [报告]
发表于 2013-11-26 17:55 |只看该作者
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带 ...

只要你自己不退群,我们就不T你,
我们群很尊重人才

论坛徽章:
4
摩羯座
日期:2013-11-24 21:55:03天秤座
日期:2013-11-25 20:54:14酉鸡
日期:2013-11-26 22:08:11天蝎座
日期:2013-12-07 07:32:45
8 [报告]
发表于 2013-11-27 12:23 |只看该作者
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带 ...


我们整个群都是写游戏的

我们的游戏 客户端是用Lua写的,服务器是 c/c++写的,
游戏的目标是向最终的用户倡导先进哲学思想,健康的人生观、价值观、世界观
游戏内容渗透了爱恨情仇

论坛徽章:
0
9 [报告]
发表于 2013-11-27 13:10 |只看该作者
你把ca和cd也都打出来看看

论坛徽章:
3
摩羯座
日期:2013-11-12 20:06:19午马
日期:2013-11-27 16:35:55双鱼座
日期:2014-04-04 19:02:30
10 [报告]
发表于 2013-11-27 14:12 |只看该作者
回复 2# OwnWaterloo


    谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP