免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
21 [报告]
发表于 2004-12-27 17:23 |只看该作者

构造函数?

我说了,那是语义上的东西,从c++的语法角度来讲是应该这样,但是实际上情况并非如此,c++为什么要在无用的东西上浪费效率呢?如果构造函数不必要,他是不会生成构造函数的.

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
22 [报告]
发表于 2004-12-27 17:25 |只看该作者

构造函数?

我的可以编译过,而且没有compile-time errors。
g++ 3.2.3( mingw )


  1. class V {
  2.     int i; // private
  3. }; // No constructor

  4. int main() {
  5.     V v, v2[10];
  6. } ///:~
复制代码


  1.         .file        "test.cpp"
  2.         .def        ___main;        .scl        2;        .type        32;        .endef
  3.         .text
  4.         .align 2
  5. .globl _main
  6.         .def        _main;        .scl        2;        .type        32;        .endef
  7. _main:
  8.         pushl        %ebp
  9.         movl        %esp, %ebp
  10.         subl        $88, %esp
  11.         andl        $-16, %esp
  12.         movl        $0, %eax
  13.         movl        %eax, -76(%ebp)
  14.         movl        -76(%ebp), %eax
  15.         call        __alloca
  16.         call        ___main
  17.         movl        $0, %eax
  18.         leave
  19.         ret
复制代码

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
23 [报告]
发表于 2004-12-27 17:28 |只看该作者

构造函数?

就是,就是,superdoctor你怎么知道阿。呵呵。
aero我投你一票。

论坛徽章:
0
24 [报告]
发表于 2004-12-27 17:31 |只看该作者

构造函数?

[quote]原帖由 "playmud"]我说了,那是语义上的东西,从c++的语法角度来讲是应该这样,但是实际上情况并非如此,c++为什么要在无用的东西上浪费效率呢?如果构造函数不必要,他是不会生成构造函数的.[/quote 发表:

如此说来,就不应该是理论上的问题了,而应该是编译器实现的问题了。

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

构造函数?

to kj501兄,你不觉得think in里的话前后矛盾吗?


  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.

复制代码

前面说了,“the compiler will automatically create one for you.”
既然编译器可以“create one for you.”,那么为什么后面又说:“will generate compile-time errors. ”呢??编译器不是已经自己产生了一个吗??

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

构造函数?

原帖由 "kj501" 发表:

如此说来,就不应该是理论上的问题了,而应该是编译器实现的问题了。

c++是一个标准,一个语言,如何实现他不同的编译器有不同的做法.
如同成员函数指针一样,有的长度是8有的是4,为啥呢?实现不同.

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

构造函数?

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

要完整的看待这句话。这名话的意思 是说在定义了构造函数同时又没有定义缺省构造函数的情况下,编译器才会出错。也就是说,只要你定义了一个构造函数,就要同时定义缺省构造函数才行。

论坛徽章:
0
28 [报告]
发表于 2004-12-27 17:45 |只看该作者

构造函数?

原帖由 "kj501" 发表:

如果你不写构造函数,系统会给你自动添加一个缺省的构造函数。这个构造函数的行为不一定是你需要的,所以还是勤快点,自己写一个空构造函数也行。


我想问题的关键在于“缺省的构造函数”这个概念,这里的缺省不是“编译器在没有构造函数的时候加上的缺省”,而是:

缺省构造函数是指不需要用户指定实参就能被调用的构造函数,这并不意味着它不接受实参,只是意味着构造函数的每个参数都有一个缺省值与之关联。
以上引自《c++primer》中文版p572
就是think一书里面也有,就在你的那篇文档里,再往上找有这么一句:
A default constructor is one that can be called with no arguments. A default constructor is used to create a “vanilla object,” but it’s also important when the compiler is told to create an object but isn’t given any details.

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
29 [报告]
发表于 2004-12-27 17:45 |只看该作者

构造函数?

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

上面的英语按照我的理解应该是这样的:
however,if any constructors are defined and there’s no default constructor,the instances of V above will generate compile-time errors.

论坛徽章:
0
30 [报告]
发表于 2004-12-27 17:46 |只看该作者

构造函数?

原帖由 "kj501" 发表:

要完整的看待这句话。这名话的意思 是说在定义了构造函数同时又没有定义缺省构造函数的情况下,编译器才会出错。也就是说,只要你定义了一个构造函数,就要同时定义缺省构造函数才行。


晕,你怎么可以这么理解呢???并且表示反对.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP