免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: dream-girl
打印 上一主题 下一主题

[函数] 构造函数? [复制链接]

论坛徽章:
0
81 [报告]
发表于 2004-12-27 22:34 |只看该作者

构造函数?

原帖由 "playmud" 发表:


inside the c++ object model上的东西吧,我只看了一遍,打算再看两遍.

是啊,就是这本书,觉得体会很深,英文水平有限就看中文版的,可惜C++水平有限--以前看国内出版的书浪费太多时间了,这本书很多没吃透,正在和effitive C++一起看。

论坛徽章:
0
82 [报告]
发表于 2004-12-27 22:36 |只看该作者

构造函数?

[quote]原帖由 "playmud"]有了自己定义的构造函数以后编译器就不自动生成默认构造函数了.[/quote 发表:


我说的就是这个意思呀!

论坛徽章:
0
83 [报告]
发表于 2004-12-27 22:39 |只看该作者

构造函数?

今晚这里很火,但好像大家没有平心静气,对别人的话多少産了误解.

竟然有産这个字?得查查.

论坛徽章:
0
84 [报告]
发表于 2004-12-27 22:41 |只看该作者

构造函数?

原帖由 "THEBEST" 发表:
紫扔行楹??睦嗖呕嵊行楸砩??有虚表才会有vptr.
你说其初始化是在ctro之前还是里面.你说ctor之前是什么时候?是进入函数体之前?如果是的话,那么vptr是在函数体中初始化的.所有的构造当然都是在构造函数中完成,你?.........


  1.         .file        "test.cpp"
  2. .lcomm __ZSt8__ioinit,16
  3.         .def        ___main;        .scl        2;        .type        32;        .endef
  4.         .text
  5. LC0:
  6.         .ascii "pause\0"
  7.         .align 2
  8. .globl _main
  9.         .def        _main;        .scl        2;        .type        32;        .endef
  10. _main:
  11.         pushl        %ebp
  12.         movl        %esp, %ebp
  13.         subl        $56, %esp
  14.         andl        $-16, %esp
  15.         movl        $0, %eax
  16.         movl        %eax, -28(%ebp)
  17.         movl        -28(%ebp), %eax
  18.         call        __alloca
  19.         call        ___main
  20.         leal        -24(%ebp), %eax
  21.         movl        %eax, (%esp)
  22.         call        __ZN1BC1Ev
  23.         movl        $LC0, (%esp)
  24.         call        _system
  25.         movl        $0, %eax
  26.         leave
  27.         ret
  28.         .align 2
  29.         .def        __Z41__static_initialization_and_destruction_0ii;        .scl        3;        .type        32;        .endef
  30. __Z41__static_initialization_and_destruction_0ii:
  31.         pushl        %ebp
  32.         movl        %esp, %ebp
  33.         subl        $8, %esp
  34.         cmpl        $65535, 12(%ebp)
  35.         jne        L3
  36.         cmpl        $1, 8(%ebp)
  37.         jne        L3
  38.         movl        $__ZSt8__ioinit, (%esp)
  39.         call        __ZNSt8ios_base4InitC1Ev
  40. L3:
  41.         cmpl        $65535, 12(%ebp)
  42.         jne        L2
  43.         cmpl        $0, 8(%ebp)
  44.         jne        L2
  45.         movl        $__ZSt8__ioinit, (%esp)
  46.         call        __ZNSt8ios_base4InitD1Ev
  47. L2:
  48.         leave
  49.         ret
  50. LC1:
  51.         .ascii ",\0"
  52.         .section        .text$_ZN1BC1Ev,"x"
  53.         .linkonce discard
  54.         .align 2
  55. .globl __ZN1BC1Ev
  56.         .def        __ZN1BC1Ev;        .scl        2;        .type        32;        .endef
  57. __ZN1BC1Ev:
  58.         pushl        %ebp
  59.         movl        %esp, %ebp
  60.         subl        $8, %esp
  61.         movl        8(%ebp), %eax
  62.         movl        %eax, (%esp)
  63.         call        __ZN1AC2Ev
  64.         movl        8(%ebp), %eax
  65.         movl        $__ZTV1B+8, (%eax)
  66.         movl        8(%ebp), %eax
  67.         movl        $20, 8(%eax)
  68.         movl        8(%ebp), %eax
  69.         movl        4(%eax), %eax
  70.         movl        %eax, 4(%esp)
  71.         movl        $__ZSt4cout, (%esp)
  72.         call        __ZNSolsEi
  73.         movl        $LC1, 4(%esp)
  74.         movl        %eax, (%esp)
  75.         call        __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
  76.         movl        %eax, %edx
  77.         movl        8(%ebp), %eax
  78.         movl        8(%eax), %eax
  79.         movl        %eax, 4(%esp)
  80.         movl        %edx, (%esp)
  81.         call        __ZNSolsEi
  82.         movl        $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
  83.         movl        %eax, (%esp)
  84.         call        __ZNSolsEPFRSoS_E
  85.         movl        8(%ebp), %eax
  86.         movl        %eax, (%esp)
  87.         call        __ZN1B4sizeEv
  88.         leave
  89.         ret
  90. .globl __ZNSt15basic_streambufIcSt11char_traitsIcEE13_S_pback_sizeE
  91.         .section        .rdata$_ZNSt15basic_streambufIcSt11char_traitsIcEE13_S_pback_sizeE,""
  92.         .linkonce same_size
  93.         .align 4
  94. __ZNSt15basic_streambufIcSt11char_traitsIcEE13_S_pback_sizeE:
  95.         .long        1
  96. .globl __ZTV1B
  97.         .section        .rdata$_ZTV1B,""
  98.         .linkonce same_size
  99.         .align 8
  100. __ZTV1B:
  101.         .long        0
  102.         .long        __ZTI1B
  103.         .long        __ZN1B4sizeEv
  104.         .section        .text$_ZN1AC2Ev,"x"
  105.         .linkonce discard
  106.         .align 2
  107. .globl __ZN1AC2Ev
  108.         .def        __ZN1AC2Ev;        .scl        2;        .type        32;        .endef
  109. __ZN1AC2Ev:
  110.         pushl        %ebp
  111.         movl        %esp, %ebp
  112.         subl        $8, %esp
  113.         movl        8(%ebp), %eax
  114.         movl        $__ZTV1A+8, (%eax)
  115.         movl        8(%ebp), %eax
  116.         movl        $10, 4(%eax)
  117.         movl        8(%ebp), %eax
  118.         movl        4(%eax), %eax
  119.         movl        %eax, 4(%esp)
  120.         movl        $__ZSt4cout, (%esp)
  121.         call        __ZNSolsEi
  122.         movl        $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
  123.         movl        %eax, (%esp)
  124.         call        __ZNSolsEPFRSoS_E
  125.         movl        8(%ebp), %eax
  126.         movl        %eax, (%esp)
  127.         call        __ZN1A4sizeEv
  128.         leave
  129.         ret
  130.         .section        .text$_ZN1B4sizeEv,"x"
  131.         .linkonce discard
  132.         .align 2
  133. .globl __ZN1B4sizeEv
  134.         .def        __ZN1B4sizeEv;        .scl        2;        .type        32;        .endef
  135. __ZN1B4sizeEv:
  136.         pushl        %ebp
  137.         movl        %esp, %ebp
  138.         subl        $8, %esp
  139.         movl        $12, 4(%esp)
  140.         movl        $__ZSt4cout, (%esp)
  141.         call        __ZNSolsEj
  142.         movl        $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
  143.         movl        %eax, (%esp)
  144.         call        __ZNSolsEPFRSoS_E
  145.         leave
  146.         ret
  147. .globl __ZTI1B
  148.         .section        .rdata$_ZTI1B,""
  149.         .linkonce same_size
  150.         .align 4
  151. __ZTI1B:
  152.         .long        __ZTVN10__cxxabiv120__si_class_type_infoE+8
  153.         .long        __ZTS1B
  154.         .long        __ZTI1A
  155. .globl __ZTS1B
  156.         .section        .rdata$_ZTS1B,""
  157.         .linkonce same_size
  158. __ZTS1B:
  159.         .ascii "1B\0"
  160. .globl __ZTV1A
  161.         .section        .rdata$_ZTV1A,""
  162.         .linkonce same_size
  163.         .align 8
  164. __ZTV1A:
  165.         .long        0
  166.         .long        __ZTI1A
  167.         .long        __ZN1A4sizeEv
  168.         .section        .text$_ZN1A4sizeEv,"x"
  169.         .linkonce discard
  170.         .align 2
  171. .globl __ZN1A4sizeEv
  172.         .def        __ZN1A4sizeEv;        .scl        2;        .type        32;        .endef
  173. __ZN1A4sizeEv:
  174.         pushl        %ebp
  175.         movl        %esp, %ebp
  176.         subl        $8, %esp
  177.         movl        $8, 4(%esp)
  178.         movl        $__ZSt4cout, (%esp)
  179.         call        __ZNSolsEj
  180.         movl        $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
  181.         movl        %eax, (%esp)
  182.         call        __ZNSolsEPFRSoS_E
  183.         leave
  184.         ret
  185. .globl __ZTI1A
  186.         .section        .rdata$_ZTI1A,""
  187.         .linkonce same_size
  188.         .align 4
  189. __ZTI1A:
  190.         .long        __ZTVN10__cxxabiv117__class_type_infoE+8
  191.         .long        __ZTS1A
  192. .globl __ZTS1A
  193.         .section        .rdata$_ZTS1A,""
  194.         .linkonce same_size
  195. __ZTS1A:
  196.         .ascii "1A\0"
  197.         .text
  198.         .align 2
  199.         .def        __GLOBAL__I_main;        .scl        3;        .type        32;        .endef
  200. __GLOBAL__I_main:
  201.         pushl        %ebp
  202.         movl        %esp, %ebp
  203.         subl        $8, %esp
  204.         movl        $65535, 4(%esp)
  205.         movl        $1, (%esp)
  206.         call        __Z41__static_initialization_and_destruction_0ii
  207.         leave
  208.         ret
  209.         .section        .ctors,"w"
  210.         .align 4
  211.         .long        __GLOBAL__I_main
  212.         .text
  213.         .align 2
  214.         .def        __GLOBAL__D_main;        .scl        3;        .type        32;        .endef
  215. __GLOBAL__D_main:
  216.         pushl        %ebp
  217.         movl        %esp, %ebp
  218.         subl        $8, %esp
  219.         movl        $65535, 4(%esp)
  220.         movl        $0, (%esp)
  221.         call        __Z41__static_initialization_and_destruction_0ii
  222.         leave
  223.         ret
  224.         .section        .dtors,"w"
  225.         .align 4
  226.         .long        __GLOBAL__D_main
  227.         .def        __ZNSt8ios_base4InitD1Ev;        .scl        3;        .type        32;        .endef
  228.         .def        _system;        .scl        3;        .type        32;        .endef
  229.         .def        __ZN1BC1Ev;        .scl        3;        .type        32;        .endef
  230.         .def        __ZN1B4sizeEv;        .scl        3;        .type        32;        .endef
  231.         .def        __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;        .scl        3;        .type        32;        .endef
  232.         .def        __ZN1AC2Ev;        .scl        3;        .type        32;        .endef
  233.         .def        __ZNSolsEj;        .scl        3;        .type        32;        .endef
  234.         .def        __ZN1A4sizeEv;        .scl        3;        .type        32;        .endef
  235.         .def        __ZNSolsEPFRSoS_E;        .scl        3;        .type        32;        .endef
  236.         .def        __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_;        .scl        3;        .type        32;        .endef
  237.         .def        __ZNSolsEi;        .scl        3;        .type        32;        .endef
  238.         .def        __ZNSt8ios_base4InitC1Ev;        .scl        3;        .type        32;        .endef
复制代码

上面是由下面的程序的来的


  1. #include <iostream>;

  2. using namespace std;
  3. class A
  4. {
  5. public:
  6.     int i;
  7.     A():i(10){cout<<i<<endl;size();}
  8.     virtual void size(){cout<<sizeof(A)<<endl;}   
  9. };

  10. class B :public A
  11. {
  12.     public:
  13.         int j;
  14.         B():A(),j(20){cout<<i<<","<<j<<endl;size();}
  15.         virtual void size(){cout<<sizeof(B)<<endl;}
  16. };
  17. int main()
  18. {
  19.     B b;
  20.     system("pause");
  21. }
复制代码

哪位牛人给分析一下vptr是在哪里初始化的??

论坛徽章:
0
85 [报告]
发表于 2004-12-27 22:43 |只看该作者

构造函数?

原帖由 "converse" 发表:


FT,我自己翻译错了,应该是如果定义了构造函数而没有定义缺省构造函数的话就会出错。


难道是我理解错了你的意思?你这不是说:
如果定义了构造函数而没有定义缺省构造函数的话就会出错。

论坛徽章:
0
86 [报告]
发表于 2004-12-27 22:47 |只看该作者

构造函数?

>;>;如果定义了构造函数而没有定义缺省构造函数的话就会出错。

这里的出错指的是kj501最开始的时候的那段代码出错,你再回去看看,原文是:


  1. The default constructor is so important that if (and only if) there are no constructors for a structure (struct or class), the compiler will automatically create one for you. So this works:

  2. //: C06:AutoDefaultConstructor.cpp
  3. // Automatically-generated default constructor

  4. class V {
  5. int i; // private
  6. }; // No constructor

  7. int main() {
  8. V v, v2[10];
  9. } ///:~

  10. If any constructors are defined, however, and there’s no default constructor, the instances of V above will generate compile-time errors.

  11. You might think that the compiler-synthesized constructor should do some intelligent initialization, like setting all the memory for the object to zero. But it doesn’t – that would add extra overhead but be out of the programmer’s control. If you want the memory to be initialized to zero, you must do it yourself by writing the default constructor explicitly.

  12. Although the compiler will create a default constructor for you, the behavior of the compiler-synthesized constructor is rarely what you want. You should treat this feature as a safety net, but use it sparingly. In general, you should define your constructors explicitly and not allow the compiler to do it for you.
复制代码


是针对这段话的翻译:
If any constructors are defined, however, and there’s no default constructor, the instances of V above will generate compile-time errors.
也怪我没有翻译全,应该是“编译出错”而且是针对这段代码的出错而不是简单的“出错”。

论坛徽章:
0
87 [报告]
发表于 2004-12-27 22:48 |只看该作者

构造函数?

原帖由 "assiss" 发表:
今晚这里很火,但好像大家没有平心静气,对别人的话多少産了误解.

竟然有産这个字?得查查.


産,厉害!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
88 [报告]
发表于 2004-12-27 22:49 |只看该作者

构造函数?

原帖由 "assiss" 发表:
今晚这里很火,但好像大家没有平心静气,对别人的话多少産了误解.

竟然有産这个字?得查查.

2 playmud:偶不懂汇编,请懂行的人来分析.如果我说错了请直接说中文.

再看上面的quote.

嘿嘿...我也看到标题,咦,怎么这么长,应该有点明堂看..哈...一进来....
一些还没搞清楚语法的某些人还在讨论问题,而且还很自信的讨论.我觉得如果不太懂或者不太肯定自己的话的话那么你先去搞清楚再来跟别人讨论(吵?)嘛,或者你至少可以带着不肯定的语气嘛,你太肯定了别人会不太自信然后就以为你是半对,自己就半错了....

大家不要误会,我的意思是如果想帮助别人最好做得好点.
我也不懂,但我不懂我不会去讨论(因为你不懂你还讨论个P?)或者至少我会用带?的句子去表达我的看法.而不是!或者.  说多了...

论坛徽章:
0
89 [报告]
发表于 2004-12-27 22:55 |只看该作者

构造函数?

原帖由 "THEBEST" 发表:
侍?而且还很自信的讨论.我觉得如果不太懂或者不太肯定自己的话的话那么你先去搞清楚再来跟别人讨论(吵?)嘛,或者你至少可以带着不肯定的语气嘛,你太肯定了别人会不太自信然后就以为你是半对,自己就半错了....

大?.........

肯定了还用讨论吗?直接说是XXXXX就行了,你们不用超了,我得对.

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
90 [报告]
发表于 2004-12-27 22:58 |只看该作者

构造函数?

呵呵,肯定了就不用讨论?上面多少人不是肯定的在说?
你知道肯定的话多么难说吗?需要多大的胆吗??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP