关闭

C++默认构造函数

标签: C++默认构造函数default constructor
537人阅读 评论(0) 收藏 举报
分类:

之前看书一直搞不懂什么时候需要自己定义默认构造,在网上查了半天也没解决自己的疑惑,网上的文章多半都是拷过来拷过去的,直到后来看到C++ Primer英文版的对这个的解释才算弄清楚了,其实人家已经说得很清楚了。以前为了图省事,找资料的时候都是找的中文的,现在觉得还不如看英文的来的直接了,所以建议有条件的同学都直接看英文的好了。

--------------------------------------------------------分割线----------------------------------------------------------------------------

 默认构造函数(default constructor)就是在没有显式提供初始化式时调用的构造函数。它由不带参数的构造函数,或者为所有的形参提供默认实参的构造函数定义。如果定义某个类的变量时没有提供初始化时就会使用默认构造函数。

如果你没有为你的类提供任何构造函数,那么编译器将自动为你生成一个默认的无参构造函数。生成的默认构造函数什么也不错,既不会讲其成员变量置零,也不会做其他的任何事情。一旦你为你的类定义了构造函数,哪怕只是一个,那么编译器将不再生成默认的构造函数。

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(如果没有定义任何构造函数,则编译器会自动为你提供一个default constructor). 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(一旦定义了任何的构造函数,如果你想在没有显示初始化的情况下创建对象,就必须提供一个自己定义的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(隐式调用默认构造函数时,不要使用括号).

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:222169次
    • 积分:3321
    • 等级:
    • 排名:第10159名
    • 原创:54篇
    • 转载:363篇
    • 译文:0篇
    • 评论:2条
    最新评论