When writing a C + + program, we declare the class type for a particular class of objects, and almost every class we declare has one or more constructors, a destructor, an assignment operator overload =, and a copy constructor. These functions control the underlying operation of the class object, ensure the initialization of the newly defined object, clean up when the object is undone, and give the object a new value. It is important to ensure that the operation of these functions is normal if the operation of the function is faulted, which can result in serious consequences.
First, compiler-generated functions by default
If we write an empty class, the compiler generates constructors, destructors, assignment operators, copy constructors for us by default.
For example, when we define
Class empty{};
It's like we wrote the following code (red is the compiler default build)
Class empty{
Public
Empty () {...} Default constructor
Empty (const empty &RHS) {...} Default copy constructor
~empty () {...} Default destructor constructor
empty& operator= (const Empty &RHS) {...} Assignment operators
empty* operator& () {...} Fetch address operator
Const empty* operator& () const{...} The const version of the fetch address operation method
};
1. Description: (1) These functions will only be created by the compiler when they are called;
( 2) Four functions are public and inline;
(3) If one of the functions is defined in the display, the compiler will not generate its corresponding default version;
(4) The custom copy constructor overrides not only the default copy constructor, but also the default constructor, the following function class constructor, which cannot be compiled
1 classEmpty2 {3 4 Public:5Empty (Constempty&e) {}6 };7 8 intMainintargcChar**argv)9 {Ten Empty A; One } A //error:no matching function for call to ' Empty::empty () ' |View Code
2. Example:
The following code causes the compiler to create the default constructor, Empty E1; Default constructor
Empty E2 (E1);//Copy constructor
E2 = e1;//assignment operator
1#include <iostream>2 using namespacestd;3 4 classempty{5 6 Public:7Empty () {cout <<"Create"<<Endl;}8Empty (ConstEmpty ©) {cout <<"Copy"<<Endl;}9empty&operator=(ConstEmpty &assig) {cout <<"assign="<<Ten Endl;} Oneempty*operator& () {cout <<"&"<<Endl;} A Constempty*operator& ()Const{cout <<"&1"<<Endl;} -~empty () {cout <<"Delete"<<Endl;} - }; the intMain () - { -Empty *e =NewEmpty ();//Create - DeleteE//Delete +Empty E0;//Create - ConstEmpty E1;//Create +Empty E2 (E1);//Copy AEmpty E3;//Create atE3 = E1;//assign= -cout << &e0 << Endl;//& 0x602080 - ConstEmpty *p = &e1;//&1 -cout << p << Endl;//0x602080 - return 0; - } in //Delete the E0,e1,e2,e3 object when it is revoked - Delete to Delete + Delete - DeleteView Code
Second, the structure function
1. Function of Constructors
A constructor is a special member function that completes some initialization of an object's properties when the object is created, and the object automatically calls its constructor when the object is created.
2. Default constructors
As described in the first section, if no constructors are defined for a class display, the compiler will automatically generate a default constructor for this class. The default constructor initializes all members of the class according to the rules that the variable initializes:
(1) for a member with a class type, the default constructor of the class itself that the member belongs to is called for initialization;
(2) The initial value of a built-in type member is dependent on how the object is defined, and if the object is defined in the global scope or defined as a static local object, those members are initialized to 0. If the object is defined in a local scope, the members are not initialized;
(3) The default constructor generally applies to classes that contain only members of the class type;
(4) because the default constructor does not initialize the members of the built-in type, you must display the constructor that defines the class.
1#include <iostream>2 using namespacestd;3 classEmpty4 {5 Public:6 intA;7 strings;8 };9 Ten intMainintargcChar**argv) One { A Empty A; -cout << a.a << Endl;//the value of output A is random -cout << a.s.size () << Endl;//S is the class type is initialized to an empty string the}View Code
3. Characteristics of the constructor function
(1) executed automatically when the object is created;
(2) The function name of the constructor is the same as the class name;
(3) No return value type, no return value;
(4) The constructor cannot be explicitly called;
4. Overloaded constructors
There is no limit to the number of constructors that can be declared for a class, as long as the formal parameters of each constructor represent unique. When defining a class object, the argument specifies which constructor to use.
C + + construction/destructor/assignment function