High-quality programming guide-C/C ++ Chapter 1 object initialization, copying and Analysis

Source: Internet
Author: User

Chapter 3 object initialization, copying, and analysis
Constructors, destructor, and assignment functions (operators) are the most basic functions of each class.
13.1 The origins of constructor and destructor
Put the object initialization work in the constructor, and put the cleanup work in the destructor. When an object is created, the constructor is automatically executed. When the object disappears, the Destructor is automatically executed. In this way, you don't have to worry about the initialization and clearing of objects.
13.2 why constructor and destructor are required
Note the difference between "initialization" and "assignment.
13.3 list of constructor member Initialization
1) First, call the constructors of the base class and pass the required parameters to them;
2) initialize the data members of this class (including the initialization of member objects );
3) complete other initialization work in the constructor body.
13.4 structure order and analysis order of Objects
The initialization order of object data members is determined only by the order they declare in the class, because this order is unique! The Destructor are executed in the reverse order of the constructor.
13.5 time for calling constructor and destructor
13.6 overload of constructors and value assignment functions
1. c ++ allows you to define multiple constructors for a class, that is, overload constructors. When we define multiple constructors for the class, we tryCodeSegments are extracted to form a non-public member function, and then called in appropriate places.
2. The class assignment function operator = () is also a copy function, and can also be overloaded. The parameter can be the current class (that is, the copy assignment operator of the class), or any other type of parameter.

13.7 example: constructor and destructor of the string class

13.8 when should we define the copy constructor and copy assignment function?
Generally, if the class contains pointer members or reference members, you should redefine the copy constructor, copy the value assignment function, and destructor.
13.9 example: copy constructor and copy assignment function of the string type
Do not forget to check auto-assigned values.
13.10 handling copy constructor and copy assignment function with a lazy Approach
That is, if you do not want to useThe compiler needs to generate functions for us.Clearly block.
13.11 how to implement basic functions of a derived class
The constructors, destructor, and assignment functions of the base class cannot be inherited by the derived class.

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.