免费注册 查看新帖 |

Chinaunix

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

[函数] 什么是默认构造函数啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-13 18:39 |只看该作者 |倒序浏览
我在看书的时候,发现并没有讲清楚默认构造函数,就来问问,默认构造函数==无参构造函数吗,谢谢

论坛徽章:
0
2 [报告]
发表于 2007-08-13 21:15 |只看该作者
Inside CPP Object Model
有讲得很清楚哈
~~~简单的讲就是"需要时"由编译器产生的构造函数

论坛徽章:
0
3 [报告]
发表于 2007-08-13 21:38 |只看该作者
我看C++primer就没讲清楚
哪有那么多书啊,能不能详细说一下啦

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2007-08-13 21:59 |只看该作者
c++ primer plus fourth edtion:

The Default Constructor
The default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, it's the constructor used for declarations like this:

Stock stock1;  // uses the default constructor

Hey, Listing 10.3 already did that! The reason this statement works is that if you fail to provide any constructors, C++ automatically supplies a default constructor. It's a default version of a default constructor, and it does nothing. For the Stock class, it would look like this:

Stock::Stock() { }

The net result is that the stock1 object is created with its members uninitialized, just as

int x;

creates x without providing it a value. The fact that the default constructor has no arguments reflects the fact that no values appear in the declaration.

A curious fact about the default constructor is that the compiler provides one only if you don't define any constructors. Once you define any constructor for a class, the responsibility for providing a default constructor for that class passes from the compiler to you. If you provide a nondefault constructor, such as Stock(const char * co, int n, double pr) and don't provide your own version of a default constructor, then a declaration like

Stock stock1;  // not possible with current constructor

becomes an error. The reason for this behavior is that you might want to make it impossible to create uninitialized objects. If, however, you wish to create objects without explicit initialization, you must define your own default constructor. This is a constructor that takes no arguments. You can define a default constructor two ways. One is to provide default values for all the arguments to the existing constructor:

Stock(const char * co = "Error", int n = 0, double pr = 0.0);

The second is to use function overloading to define a second constructor, one that has no arguments:

Stock();

You can have only one default constructor, so be sure that you don't do both. (With early versions of C++, you could use only the second method for creating a default constructor.)

Actually, you usually should initialize objects in order to ensure that all members begin with known, reasonable values. Thus, the default constructor typically provides implicit initialization for all member values. Here, for example, is how you might define one for the Stock class:

Stock::Stock()
{
    strcpy(company, "no name");
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}

Tip


When you design a class, you usually should provide a default constructor that implicitly initializes all class members.


After you've used either method (no arguments or default values for all arguments) to create the default constructor, you can declare object variables without initializing them explicitly:

Stock first;                // calls default constructor implicitly
Stock first = Stock();      // calls it explicitly
Stock *prelief = new Stock; // calls it implicitly

However, don't be misled by the implicit form of the nondefault constructor:

Stock first("Concrete Conglomerate");      // calls constructor
Stock second();                            // declares a function
Stock third;                               // calls default constructor

The first declaration calls the nondefault constructor, that is, the one that takes arguments. The second declaration states that second() is a function that returns a Stock object. When you implicitly call the default constructor, don't use parentheses.

论坛徽章:
0
5 [报告]
发表于 2007-08-13 22:38 |只看该作者
c++ primer plus fourth edtion:

The Default Constructor
The default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, it's the constructor used for declarations like this:

Stock stock1;  // uses the default constructor

Hey, Listing 10.3 already did that! The reason this statement works is that if you fail to provide any constructors, C++ automatically supplies a default constructor. It's a default version of a default constructor, and it does nothing. For the Stock class, it would look like this:



看懂了,不过默认构造函数==无参构造函数吗还是不确定
谢谢版主

论坛徽章:
0
6 [报告]
发表于 2007-08-13 23:24 |只看该作者
无须输入参数的构造函数都可以作为默认构造函数,并不等于无参构造函数.
如果一个构造函数带有参数,但是这个参数具有默认值,那么这个构造函数也可以作为默认构造函数。

论坛徽章:
0
7 [报告]
发表于 2009-02-25 22:30 |只看该作者
原帖由 hantom 于 2007-8-13 22:38 发表
c++ primer plus fourth edtion:

The Default Constructor
The default constructor is the constructor used to create an object when you don't provide explicit initialization values. That is, it's ...

上面说了啊,default constructor有两种(……your own default constructor. This is a constructor that takes no arguments):
1)One is to provide default values for all the arguments to the existing constructor:
Stock(const char * co = "Error", int n = 0, double pr = 0.0);
2)The second is to use function overloading to define a second constructor, one that has no arguments:
Stock();
有一点注意的时候两者不能同时使用:
You can have only one default constructor, so be sure that you don't do both. (With early versions of C++, you could use only the second method for creating a default constructor.)
This is a constructor that takes no arguments:这个指的是调用的时候不带参数。

论坛徽章:
0
8 [报告]
发表于 2009-02-26 11:25 |只看该作者
不用过于深究,

论坛徽章:
0
9 [报告]
发表于 2009-02-26 15:53 |只看该作者
Google 以下关键字:
Overriding default constructor
有关默认,你其实再深入一步就能想明白了。为什么叫默认?就是你没做什么的时候,某个人跑过来对你说:啊!某某某,你要的啥啥啥,我已经帮你准备好了!你这时候既可以选择接受下来,也可以选择自己造一个,自己造的时候就有更大的余地来定制,也更灵活。
应用到构造函数的概念上,也是一个道理。举个例子,你哪天忘记写构造函数了怎么办?或者,你觉得写一个很麻烦,不想写,怎么办?没关系,编译系统会为你提供相应的构造函数。这就叫做默认构造函数。

论坛徽章:
0
10 [报告]
发表于 2009-02-26 17:20 |只看该作者
什么是默认构造函数
什么是无参构造函数
什么是带参构造函数
什么是拷贝构造函数
什么是赋值构造函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP