C ++ designs a class that cannot be held in the lazy state.

Source: Internet
Author: User

1. constructor/default constructor: the question about how to initialize an object is how to fill in the content of a new data type, whether explicit it or implicit is required or not is required. It is generally recommended that explicit it be emphasized because implicit type conversion is always reassuring. There are many details to consider, including the default parameter value design, data member initialization list, and so on... Instantiate an object that runs a corresponding constructor to ensure that the data member content of the instantiated object is controllable.
Class
{
A ();
A (X _ x, Y _ y): x (_ x), y (_ y ){}
//...
Private:
X x;
Y y;
//...
};


2. copy constructor/assign value function: This involves thinking about how to accurately copy an object. Especially when there is a pointer to a free-space resource (heap space) in the class, it cannot depend on the copy constructor/value assignment function generated by the compiler by default, by default, objects are replicated one by one. The result is undefined because the Destructor may analyze the same resource multiple times! For example, the transfer of ownership of smart pointers may even lead to a trap of disordered business logic.
The general form is:
Class
{
A (const A & rhs); // copy the constructor
A & operator = (const A & rhs); // value assignment function
//...
};

In this case, you need to be clear-headed and define what you think is best for these replication operations. Generally, the copy constructor and the value assignment function are different. The root cause is that. The copy constructor completes the initialization of the uninitialized storage area, while the value assignment function correctly processes an object with a good structure. Generally, you can optimize the value assignment function. The policy is to prevent auto-assignment, delete old resources, and copy new data. Generally, each non-static member must be copied.

3. destructor: The first thing C ++ programmers need to learn is to take care of their own programs! The first is to manage the applied resources and promise to recycle resources that are no longer used. a c ++ object model or a key model for managing Custom Data Type resources is the destructor, the compiler ensures that the destructor of the object is executed at the end of the object's life cycle (if not clearly defined, it will help you generate one). This is a language mechanism.
! Either follow the rules or take responsibility for the consequences. The Destructor can think that she is the aunt who cleans the hotel. When you finish a passionate party in the hotel room, you need someone to help you clean it up, right? Otherwise, don't worry after check-out. Isn't it a mess? Therefore, we promise that a well-designed class must have a corresponding mechanism for releasing resources. One of the keys is the destructor.
Class
{
Virtual ~ A ();
};


Author: temotemo

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.