Constructors, destructors such as C + + learning

Source: Internet
Author: User

  In a C + + class, there will be one or more constructors, a destructor, an assignment operator. Even if we define them in our own classes, the compiler declares a default constructor, a destructor, and an assignment operation operator. For example:

1 //declaring an empty class2 classempty{};3 4 //but this empty class is equivalent to the following class5 classEmpty6 {7Empty () {...};//default constructor8Empty (ConstEmpty & RHS) {...}//copy Constructor9~empty () () {...}// DestructorsTen      OneEmptyoperator* (ConstEmpty &RHS)  {.........}  A};

It is important to note that only when these functions are called will they be created by the compiler.

If we have declared a constructor, the compiler will no longer create a default constructor.

  The following is a detailed description of constructors and destructors.

1. Constructors

A constructor (constructor) is a special member function with the same name as a class that is used primarily to initialize the data members of an object. The defined form is as follows:

1 class   X2{3   //  ... .. 4      X ();    // no parameter constructor 5      X (...);  // with the parameter constructor, the constructor supports overloading 6     7 //     8

  

Constructors are declared and defined in the same way as other member functions of a class, you can define constructors within a class, or you can declare a constructor in a class and then define it outside of the class. The constructor is defined outside the class in the following form:

1 x:: X (...) 2 {3//... .. 4 }

  The constructor has several characteristics:

(1) The constructor has the same name as the class

(2) The constructor does not have a return type, and void is not

(3) constructors can be overloaded

(4) The constructor is automatically called by the system, and it is not allowed to be displayed in the program

(5) The invocation time of the constructor is the first time after the object is defined, that is, the constructor is the first called function of an object

(6) When you define an array of objects or create a dynamic object with new, you also call the constructor. But when you define an array object, you must have a parameterless constructor

(7) Constructors should generally be defined as a common member (or, of course, can be defined as private, but cannot be accessed outside the class.) The singleton mode is used to privatize the constructor)

  At the same time, you need to pay attention to several points:

(1) The member initialization order in the constructor initialization list is the same as the order in which they are declared in the class, regardless of the order in which they are initialized in the list. Such as:

1 tdate::tdate (intint int y): Month (m), Day (d), year (y) {}2 Tdate::tdate (intint int y): Year (y), month (m), day (d) {}3 tdate::tdate (intintint  y):d ay (d), year (y), month (m) {} 4 // The above three constructors are exactly the same

(2) The constructor initialization list is executed before the statement in the constructor body

(3) The following class members must be initialized with a member initialization list: constant member, reference member, class object member, derived class constructor call to base class constructor

1.1 Default constructors

The default constructor is the constructor that does not need to display the supplied arguments. In some cases, you must use the default constructor to define the object (such as an array of objects)

If a class does not have any constructors defined, the compiler will generate a default constructor for it when needed, and it is only responsible for creating the object without doing any initialization work.

When you create an object with a default constructor, if you create a global object or a static object, the object's bit pattern is all 0 (which can be understood as initializing all data members to 0), and if you are creating a local object that does not initialize the object data member, the object data member is unknown.

Note: The system generates a default constructor only if no constructors are defined for the class. Once a constructor of any form is defined, the system will no longer produce a default constructor.

We can redefine the default constructor to provide the initial value for the object's data members. We can also provide default values for parameters.

1.2 Overloaded constructors

Overloaded constructors must have different function prototypes (that is, the number of arguments, parameter types, or parameter order cannot be exactly the same)

1.3 Copy Constructors

A copy constructor is a special constructor that initializes a new object based on an existing object.

If you do not define a copy constructor for a class, the C + + compiler will produce a default copy constructor with minimal functionality, if required, in the form of: x::x (const x&) {}

  The default copy constructor implements the replication of members in a bitwise copy (bit-by-bit) manner. Bitwise replication is the copy of the values of each data member of an object into the target object. The default constructor works well when there are no data members that involve pointer types. However, when a class has a pointer-type data member, the default copy constructor often produces a pointer hanging problem. If a class has a data member of the pointer type, it should be provided with a custom copy constructor (that is, overloading)

  Note : The parameters of the copy constructor are often references to this class of Const type objects (X::X (const x&) {})

2. Destructors

  A destructor (destructor) is another special member function that has the same name as a class, and is used instead of a constructor to perform cleanup of objects at the end of an object's lifetime.

The name of the destructor consists of "~" + "class name" in the form of the following:

1 classX2 {3    Private:4    //............5    Public:6~x ();// Destructors7X ();//no parameter constructor8X (...);//constructors with a parameter overload9 //.........Ten}

  Destructors have the following characteristics :

(1) The name of the destructor is preceded by a "~" in the class name and cannot be another name

(2) The destructor has no return value type (void), no parameter table

(3) Destructors cannot be overloaded, a class can have only one destructor

(4) Destructors can only be called automatically by the system and can no longer be displayed in the program call destructor

(5) If more than one object ends the lifetime at the same time, C + + invokes the destructor in the reverse order of the calling constructor

(6) Each class should have a destructor, and C + + will produce a minimized default destructor if no destructor is displayed (X::~x ())

(7) Constructors and destructors can all be inline functions

(8) Under normal circumstances, destructors should be set to public members

Constructors, destructors such as C + + learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.