How to use C ++ class Constructor

Source: Internet
Author: User

The following describes how to use C ++ class constructor. The so-called C ++ class is a blueprint or prototype that defines the variables and methods of all objects of the same class, members of the C ++ class need to dynamically open up heap memory. If we do not customize the copy constructor, the system will handle it by itself.

We have learned about constructor and destructor of classes. For common objects, copying between them is very simple. For example: the objects of the defined classes are also objects. No one can prevent us from copying them in the following ways, for example:

 
 
  1. #include <iostream>    
  2. using namespace std;    
  3.     
  4. class Test    
  5. {    
  6. public:    
  7.     Test(int temp)    
  8.     {    
  9.         p1=temp;    
  10.     }    
  11. protected:    
  12.     int p1;    
  13.     
  14. };    
  15.     
  16. void main()    
  17. {    
  18.     Test a(99);    
  19.     Test b=a;    

Common objects and class objects are the same objects, and their features are similar. C ++ class objects have member variables, while common objects do not, when the same replication method occurs on different objects, the system performs different operations on them. For C ++ class objects, class objects of the same type are copied through the constructor.

In the above Code, we did not see the copy constructor, And the replication work was also completed. Why? Because when a class does not have a custom copy constructor, the system automatically provides a default copy constructor to complete the copy.

Next, we define a copy constructor that is the same as the default copy constructor of the system to illustrate the situation (for example, the above Code, let's see how it works internally!

The Code is as follows:

 
 
  1. # Include<Iostream>
  2. Using namespace std;
  3. Class Test
  4. {
  5. Public:
  6. Test (int temp)
  7. {
  8. P1=Temp;
  9. }
  10. Test (Test & c_t) // here is the custom copy constructor.
  11. {
  12. Cout<<"Enter the copy constructor"<<Endl;
  13. P1=C_t. P1; // if this statement is removed, the replication cannot be completed.
  14. }

In the above Code, Test (Test & c_t) is our custom copy constructor. The name of the copy constructor must be the same as that of the C ++ class, the function form parameter is a reference variable of the C ++ type and must be a reference. When another newly constructed object is initialized with a user-defined C ++ class object that has already been initialized.

The copy constructor is automatically called. If you do not customize the copy constructor, the system will provide a default copy constructor to complete this process. The core statement for copying the code above is to copy the p1 = c_t.p1 In the constructor through Test (Test & c_t.

If this code is removed, the p1 attribute of object B will get an unknown random value. Many people will ask about the above Code, since the system automatically provides a default copy constructor to process replication, it makes no sense to define a copy constructor.

  • Play C ++ in a short time
  • How to define C ++ variables in C ++?
  • Analysis and definition of C ++ Data Types
  • C ++ Design Rules
  • How does C ++ Test automatically generate a function?

Yes. In general cases, this is indeed unnecessary. However, in a write condition, members of the C ++ class need to dynamically open up heap memory, if we do not customize the copy constructor and let the system process it by ourselves, it will lead to confusion about the ownership of the heap memory, the heap address opened at one end originally belongs to object.

Because of the replication process, object B obtains the heap address that has been opened up by object a. Once the program generates an analysis structure and releases the heap, it is impossible for the computer to know who the address actually belongs, A running error occurs when two destructor occurs consecutively.

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.