How to define C ++ Constructor

Source: Internet
Author: User

C ++ constructor is a special method used to initialize an object when creating an object, that is, assigning an initial value to the object member variable, whether it is a developer, Project Manager, or tester, having mastered C ++ constructor makes programming easier and easier.

The C ++ constructor initialization list starts with a colon, followed by a list of data members separated by commas (,). Each data member is followed by an initialized form in parentheses. For example:

 
 
  1. Class CExample {
  2.  
  3. Public:
  4.  
  5. Int;
  6.  
  7. Float B;
  8.  
  9. // List of constructor Initialization
  10.  
  11. CExample (): a (0), B (8.8)
  12.  
  13. {}
  14.  
  15. // Internal assignment of constructor values
  16.  
  17. CExample ()
  18.  
  19. {
  20.  
  21. A=0;
  22.  
  23. B=8. 8;
  24.  
  25. }
  26.  
  27. };

In the above example, the results of the two C ++ constructors are the same. The constructor above uses the constructor in the initialization list to explicitly initialize class members. The constructor that does not use the initialization list assigns values to class members, no Explicit initialization is performed.

There is no major difference between initialization and assignment for built-in type members, like any of the above constructors. For non-built-in type member variables, we recommend that you use class constructor to initialize the list to avoid two constructor operations. However, you must use a constructor with an initialization list:

1. The member type is a class without a default constructor. If the display initialization type is not provided, the compiler implicitly uses the default constructor of the member type. If the class does not have the default constructor, the compiler will fail to use the default constructor.

2. const member or reference type member. Because the const object or reference type can only be initialized, they cannot be assigned a value. What is the meaning of initializing data members and assigning values to data members? What is the difference? First, the data members are classified by type and described as follows:

1. built-in data type, composite type pointer, reference)

In the member initialization list and in the C ++ constructor body, the performance and results are the same.

2. User-Defined type class type)

The results are the same, but the performance varies greatly. Because the data member objects of the class type have been constructed before they enter the function body, that is, the object construction work is performed at the member initialization list. Call the C ++ constructor after entering the function body.

  • Analysis of C ++ constructor in C ++
  • Exploring open-source C ++ Libraries
  • Mode description of the VC ++ Development Environment
  • In-depth description of C ++ open source program history
  • How to Learn C ++ applications correctly

Assign values to constructed class objects and call a copy assignment operator. If not, assign values to members by default provided by the compiler. You may think that the above Code will first do m_y = I, then m_x = m_y, and finally they have the same value. But the compiler first initializes m_x and then m_y, because they are declared in this order.

The result is m_x, which will have an unpredictable value. There are two ways to avoid it. One is to declare members in the order you want them to be initialized, and the other is, if you decide to use the initialization list, these members are always listed in the order they are declared. This will help eliminate confusion.

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.