Chinaunix

标题: C++的虚表到底是怎么玩的?求指教 [打印本页]

作者: mci2004    时间: 2013-11-26 09:35
标题: C++的虚表到底是怎么玩的?求指教
  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中的虚表指针呢?



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


作者: OwnWaterloo    时间: 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. }
复制代码

作者: _BlueGuy__    时间: 2013-11-26 17:05
OwnWaterloo 发表于 2013-11-26 16:57
建议将代码改成这样再去试:

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

305369284

作者: OwnWaterloo    时间: 2013-11-26 17:44
回复 3# _BlueGuy__

我不做游戏、不谈哲学、不玩Lua、不写web服务器、不用学也没什么机会用C/C++。外带也不乱搞男女关系。
即使加进去过不了一周就会屏蔽群消息 —— 群里至少有3位能作证我这不是在乱说。
所以还是算了吧。啊。
作者: _BlueGuy__    时间: 2013-11-26 17:48
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

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

之前不是听说你在做网站?然后又在做嵌入式
作者: _BlueGuy__    时间: 2013-11-26 17:52
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

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

屏蔽群消息没关系,我们群基本不说话
作者: _BlueGuy__    时间: 2013-11-26 17:55
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

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

只要你自己不退群,我们就不T你,
我们群很尊重人才
作者: _BlueGuy__    时间: 2013-11-27 12:23
OwnWaterloo 发表于 2013-11-26 17:44
回复 3# _BlueGuy__

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


我们整个群都是写游戏的

我们的游戏 客户端是用Lua写的,服务器是 c/c++写的,
游戏的目标是向最终的用户倡导先进哲学思想,健康的人生观、价值观、世界观
游戏内容渗透了爱恨情仇
作者: syre    时间: 2013-11-27 13:10
你把ca和cd也都打出来看看

作者: mci2004    时间: 2013-11-27 14:12
回复 2# OwnWaterloo


    谢谢!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2