How to better perform C ++ Constructor

Source: Internet
Author: User

Constructor is a special method in the development process. It is mainly used to initialize an object when an object is created, that is, to assign an initial value to an object member variable, when a C ++ constructor is used in C ++, whenever a class or structure is created, its constructor is called.

However, after the program passes the compilation check, it does not mean that the error does not exist. In the "error" family, the "syntax error" status can only be regarded as a younger brother. High-level errors are often hidden in depth, just like a sly criminal. It is not easy to catch him.

Based on experience, many program errors that are hard to detect are caused by the variable being correctly initialized or cleared, and initialization and cleanup are easily forgotten. Stroustrup fully considers this problem when designing the C ++ language and solves it well: it puts the initialization of the object in the C ++ constructor, put the cleanup work in the destructor. When an object is created, the constructor is automatically executed. When an object dies, the Destructor is automatically executed. You don't have to worry about object initialization and cleanup.

The name of the constructor and the Destructor cannot start at will, and must be recognized by the compiler before it can be automatically executed. The naming method of Stroustrup is simple and reasonable: the constructor and destructor have the same name as the class. because the purpose of the constructor is opposite to that of the constructor, the prefix '~ 'To show the difference.

Except for the name, the constructor and the Destructor have no return value type, which is different from the void function. The mission of C ++ constructor and destructor is very clear, just like birth and death. If they have return value types, the compiler will be overwhelmed. In order to prevent out-of-the-box branches, it is simply specified that there is no return value type. The above reference [Eekel, p55-p56])

The C ++ constructor has a special initialization method called "initialization expression Table ). The initialization table is located after the function parameter table, but before the function body. This indicates that the initialization work in the table occurs before any code in the function body is executed.

Rules for using constructors to initialize tables:

If the class has an inheritance relationship, the derived class must call the base class constructor in its initialization table.

For example:

 
 
  1. Class F
  2.  
  3. {
  4.  
  5. Public:
  6.  
  7. F (int x, int y); // Constructor
  8.  
  9. Private:
  10.  
  11. Int m_x, m_y;
  12.  
  13. Int m_ I, m_j;
  14.  
  15. }

Class data members can be initialized using the initialization table or function body assignment method, the efficiency of these two methods is not exactly the same, non-Internal data type member objects should be initialized in the first way to achieve higher efficiency.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.