C ++ constructor, destructor, and assignment function

Source: Internet
Author: User

C ++ constructor, destructor, and assignment function

When writing a C ++ program, we declare the class type for a specific class of objects, almost every class we declare has one or more constructor, one destructor, one value assignment operator overload =, and a copy constructor. These functions control the basic operations of class objects, ensure the initialization of the newly defined object, complete the cleaning work when the object is revoked, and assign new values to the object. If the operation of these functions fails, it will lead to serious consequences. Therefore, it is very important to ensure that the operation of these functions is normal.

1. Default functions generated by the compiler

If we compile an empty class, the compiler will generate constructor, destructor, value assignment operator, and copy constructor for us by default.

For example, when we define

Class Empty {};

As if we have written the following code (Red is generated by the compiler by default)

Class Empty {

Public:

Empty () {...} // default constructor

Empty (const Empty & rhs) {...} // default copy constructor

~ Empty () {...} // default destructor

Empty & operator = (const Empty & rhs) {...} // value assignment operator

Empty * operator & () {...} // obtains the address operator.
Const Empty * operator & () const {...} // The const version of the address fetch Algorithm

};

1. Note: (1) these functions are created by the compiler only when they are called;

(2) All four functions are public and inline;

(3) If a function is defined, the compiler will not generate the corresponding default version;

(4) The Custom copy constructor will not only overwrite the default copy constructor, but also overwrite the default constructor. The following function class constructor cannot be compiled.

  

1 class Empty 2 {3 4 public: 5 Empty (const Empty & e) {} 6}; 7 8 int main (int argc, char ** argv) 9 {10 Empty a; 11} 12 // error: no matching function for call to 'empty: Empty () '|View Code

 

2. instance:

The following code allows the compiler to create the default constructor Empty e1; // default constructor

Empty e2 (e1); // copy the constructor

E2 = e1; // value assignment operator

1 # include <iostream> 2 using namespace std; 3 4 class Empty {5 6 public: 7 Empty () {cout <"create" <endl ;} 8 Empty (const Empty & Copy) {cout <"copy" <endl;} 9 Empty & operator = (const Empty & Assig) {cout <"assign =" <10 endl;} 11 Empty * operator & () {cout <"&" <endl ;} 12 const Empty * operator & () const {cout <"& 1" <endl;} 13 ~ Empty () {cout <"delete" <endl ;}14}; 15 int main () 16 {17 Empty * e = new Empty (); // create18 delete e; // delete19 Empty e0; // create20 const Empty e1; // create21 Empty e2 (e1); // copy22 Empty e3; // create23 e3 = e1; // assign = 24 cout <& e0 <endl; // & 0x60208025 const Empty * p = & e1; // & 126 cout <p <endl; // 0x60208027 return 0; 28} 29 // e0, e1, e2, delete 30 delete31 delete32 delete33 delete when the e3 object is revokedView Code

 

Ii. Constructor

1. Role of Constructor

Constructor is a special member function used to complete initialization and other operations on object attributes when an object is created. when an object is created, the object automatically calls its constructor.

2. default constructor

As described in section 1, if no constructor is defined for a class display, the compiler automatically generates default constructor for this class. The default constructor initializes all members of the class based on the variable initialization rules:

(1) For a member of the class type, the default constructor of the class to which the member belongs is called for initialization;

(2) The initial values of built-in type members depend on how the objects are defined. If the objects are defined in the global scope or are defined as static local objects, these members are initialized to 0. If the object is defined in a local scope, these members are not initialized;

(3) The default constructor is generally applicable to classes that only contain members of the class type;

(4) because the default constructor does not initialize members of the built-in type, the constructor of the defined class must be displayed.

1 # include <iostream> 2 using namespace std; 3 class Empty 4 {5 public: 6 int a; 7 string s; 8}; 9 10 int main (int argc, char ** argv) 11 {12 Empty a; 13 cout <. a <endl; // the value of output a is random 14 cout <. s. size () <endl; // s indicates that the class type is initialized as an empty string 15}View Code

3. Features of Constructor

(1) automatically executed when the object is created;

(2) The name of the constructor is the same as that of the class;

(3) No return value type or return value;

(4) constructors cannot be explicitly called;

4. Overload Constructors

There is no limit on the number of constructors that can be declared for a class, as long as the form parameters of each constructor represent unique. When defining a class object, the real parameter specifies which constructor to use.

 

 

 

 

  

 

      

      

    

  

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.