About native people in C + + classes: constructors, copy constructors, destructors

Source: Internet
Author: User

when we beginner C + +, we may have some doubts about the constructor of the class, the copy constructor, and the destructor. Organize the following (personal opinion, if there is a mistake, also look at it.) ):

1. Constructors

According to the definition of the constructor, it is to initialize the data member of the class or the object of the inline class, so its parameter table should be the type of object it is initializing. Constructors are divided into three categories: default constructors, constructors, delegate constructors.

default constructor

The default constructor has no return value, no parameter table, no function body, and if no constructor is explicitly defined within the class, the default constructor is automatically generated, and if the constructor is already defined, but the default constructor is still required, you can add the default constructor parameter table after it:

Point () = default;

Tip: Even if a constructor is already defined, it is a good idea to define a default constructor to prepare for a rainy-out.

constructor Function

Ordinary constructors come in two forms: with an initialization list, without an initialization list, and generally without much difference, but there are several special cases where you must use the form with an initialization list: If the member you want to initialize is a reference, a const type, or a class type that provides a default constructor, Must be initialized through the constructor's initial list of values. For this:

Class Point

{public:

Point (int a,int B): X (a), Y (b) {}

Private

int x, y;

};

If the main function has the following statement: Point P = point, either as a coercion type conversion or as a call to a constructor. Not only the class, but even the built-in data types, such as int, float, double, etc., if there is an int a = int (3.14); Such a statement, can be regarded as coercion type conversion, can also be seen as the call of the constructor of int, some people will have doubts: int is the data type, how can there be a constructor function? I would like to use the factory function in Python to explain the more appropriate, shaped like int () in Python called the factory function, listen to the name to know that the factory is a lot of production meaning, is the production of int type Data Factory. That's a good understanding.

Delegate Constructors

Delegate constructors are not commonly used, as the name implies, a delegate constructor that delegates a constructor's task to another constructor. Delegate constructors, like constructors, have an initial list of values and function bodies such as:

Class point//DOT Classes

{public:

Point (int a,int B): X (a), Y (b) {}//constructor with parameter

Point (): Point (0,0) {}//delegate constructor, default constructor delegate constructor initialized data member with two int parameters

Private

int x, y;

};

2. Copy Constructors

The parameter of the copy constructor is a reference to the object of this class, and the copy construct, as the name implies, is to initialize another object of this class with the object of this class, and the formal parameter is of course a reference to this class of objects. Class inheritance is no exception, the parameters of the derived class's copy constructor are also references to derived class objects, and, depending on the type compatibility rules, objects of the derived class can initialize the base class object, so this is allowed. However, it is not safe for a base class object to initialize a derived class object, because the derived class contains members of the base class (except for the base class's constructors, copy constructors, and destructors), so the scope of the derived class is greater than the base class. In a word: large can be initialized small, but small can not initialize large (dynamic_cast type conversion is the same reason).

Copy constructors in the final analysis, the construction of the structure, the wood has found that the constructor and copy constructors look alike, they have a lot in common. Therefore, the copy constructor can be regarded as an overloaded form of the constructor, the difference is that their parameter table is different, the system will call the constructor or copy constructor according to the different types of arguments.

3. Destructors

The destructor is very simple, it has no return value, no parameter table, no function body, the function name is the same as the class name, and the function name needs to be added a ~ to make it easier for the default constructor to differentiate. In general, we don't have to be too concerned with destructors, but one thing to note is that when implementing the polymorphism of a class, it is best to declare the destructor of the base class and derived class as virtual, so that the requested heap space is properly freed when dynamic memory is used.


About native people in C + + classes: constructors, copy constructors, destructors

Related Article

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.