免费注册 查看新帖 |

Chinaunix

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

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

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

构造函数?

[quote]原帖由 "aero"]^_^,没有构造函数,你的对象是怎么构造的啊?总得要生出来吧。[/quote 发表:

lippman这么说得.生出来是编译器的事.
chass A
{

public:
      fun(){cout<<"hello world!"<<endl;}
}
没必要有构造函数和析构函数

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

构造函数?

实际上构造函数的调用是在data member初始化以后的

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

构造函数?

我今天刚刚考完C++,我们老师也没讲这一部分,可是考试时考了好几倒题,我只几天看了好几本书,还是不太明白,各位大侠能不能给我介绍几本好书啊!!!小妹这里谢谢了!!!!!!!!

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

构造函数?

aero
我记得你曾经不只一次在帖子里表达过你不是很熟悉C++
所以,我认为对于不熟悉的领域最好是虚心接受,而不是挑骨头捡刺。

编译器生成的构造函数只保证编译器的需要,而不是人需要的。所以怎么生成对象,那也是编译器决定的。
事实上,最初的C++编译器是将C++代码翻译成C代码。C里面没有对象class的概念,所以,C怎么生成对象,C++就怎么生成对象(在不需要编译器介入的情况下)。

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

构造函数?

原帖由 "gvim" 发表:
aero
我记得你曾经不只一次在帖子里表达过你不是很熟悉C++
所以,我认为对于不熟悉的领域最好是虚心接受,而不是挑骨头捡刺。

编译器生成的构造函数只保证编译器的需要,而不是人需要的。所以怎么生成对象,那?.........

兄台怎么这么大火气啊?

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

构造函数?

^_^,是,虚心接受。
  
不过,我真的没挑骨头挑刺的。俺从来不挑食。^_^。说笑了,有得罪的。还望包涵小弟。

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

构造函数?

:( 火气是大了点,有点烦心的事情,望见谅。

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

构造函数?

原帖由 "playmud" 发表:


我不这么认为,构造函数和析构函数只是语义上的东西,实际上没有必要的话编译器根本不给你生成.

我对构造函数的认识主要来自于《Thinking in C++>;这本书,
http://www.gcek.net/ref/books/sw/cpp/ticppv1/chapter06.asp
下面是我摘录的部分内容:
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:

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

class V {
  int i;  // private
}; // No constructor

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

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

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.

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.

如果你的说法成立,那么书上说的应该是错误的啦,是吗?

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

构造函数?

[quote]原帖由 "gvim"]:( 火气是大了点,有点烦心的事情,望见谅。[/quote 发表:


烦心的事?  不是想和aero争准版主的位置吧  
哈哈

开个玩笑

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

构造函数?

原帖由 "superdoctor" 发表:


烦心的事?  不是想和aero争准版主的位置吧  
哈哈

开个玩笑

      
这个起刺的,咔咔咔。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP