Objective C ++ constructor destructor assignment operator

Source: Internet
Author: User

In the course of reading Objective C ++, I have been sighing for countless times. This is a good fucking write. Therefore, I decided to write the content of this book into five blogs based on my own understanding. I think whether or not you understand these terms is worth writing down. The following indexes correspond to the chapters in the book.

 

11: if the class is dynamically configured with memory, declare a copy constructor and an assignment operator for this class.

12: Use initialization to replace assignment in constructor.

13: The initialization order of members in the initialization list should be the same as the declared order in the class.

14: base class has virtual destructor.

15: Let operator = return the reference of * This

16: assign values to all data member in operator =

17: In operator =, check whether you assign a value to yourself.

 

11: if the class is dynamically configured with memory, declare a copy constructor and an assignment operator for this class.

By default, copy constructor and operator = Do not assign values to each data member of the class. Instead, they are a simple reference that directs the objects on the left to the objects on the right. No more actions. If the class dynamically allocates memory, for example, the object on the left is originally directed to a piece of memory A, and the object on the left is now directed to the memory B pointed to by the object on the right, there is no other object pointing to memory a anymore. Because it is dynamically allocated and cannot be recycled by itself, there is a memory leak, and there are two objects pointing to the same memory, if one of the objects has its own scope, the Destructor will be called automatically, and the dynamically allocated memory will be recycled. Now the other object points to a recycled memory, an unknown exception occurs when you call the data of this object. It is worth mentioning that even if an object does not point to any address or the address it points to has been recycled, when calling its method, as long as its method does not use its data member (it certainly does not exist), there will be no problem, because the Method Memory is allocated together with the object type, when instantiating an object, the system does not allocate memory for the method. Instead, it only allocates memory for data member and some other pointers, such as pointers to the parent class and virtual tables.

As described in section 11, if the two methods are not declared, problems may also occur during method calling. When this object is called as a value passing method, a temporary variable is generated, this temporary variable references this object. When the method execution is complete, this temporary variable is out of its scope and the Destructor is called, and this object is destroyed. Therefore, you must follow this rule.

 

12: Use initialization to replace assignment in constructor.

Object Construction is divided into two phases:

1: Data member is initialized.

2: The called constructor is executed.

If you assign values to data member in the constructor, you must first call the constructor of data member. If you do not assign an initial value to data member, the default constructor is called, if you assign an initial value to call copy constructor, but my compiler does not allow data member to assign an initial value when defining it, it is to call the default constructor, when you assign values to data member in the constructor, Calling operator = is equivalent to calling constructor and operator = once, while initialization only calls copy constructor once, because data member has been assigned a value for data member during initialization, you do not need to assign a value for data member in the constructor. You will often encounter this interview question: when defining a data member, you can give it an initial value and assign another value to the constructor. What is the current value of this data member? What is it? There is also a data member in the base class, which is assigned multiple values. What is the final value? Remember that the constructor of the parent class is executed before the constructor of the subclass, and the initialization parameter is executed before the constructor.

In a word, Initialization is more efficient than assigning values in constructors. If data member is large and needs to be initialized to the same value, and the efficiency is not that important, you can assign values to equations in the constructor, Which is clearer. Efficiency is not always the first,CodeIs also very important. Do you remember the 82 rule? I once wrote an if statement and was criticized in the code review, on the grounds that 1 million access may affect efficiency. I have met this situation in two companies, this is why millions of accesses affect efficiency.

 

13: The initialization order of members in the initialization list should be the same as the declared order in the class.

Sometimes the initialization of data member depends on other data member, so the initialization sequence of data member must be clarified. The initialization sequence of data member is irrelevant to the sequence in Initialization. It is only related to the sequence they define. The first defined data member will be initialized first in initialization and analyzed later in destructor, initialize the data member first and then analyze the structure. Therefore, the base data member is the same as the variables in the stack. If a class inherits multiple classes, the initialization sequence of base data member is determined by the inheritance sequence.

 

14: base class has virtual constructor.

In the inheritance relationship, whether the method of the parent class is called or the method of the subclass is called. This dynamic implementation is determined by the virtual function, classes that contain virtual functions all have a virtual function table. If the methods in the parent class are virtual, if no subclass does not override this method, it is directly inherited, no matter whether the subclass or parent class calls this method, the result is the same. If the subclass overwrites this method, the virtual table of the subclass stores the address of the subclass method, if a parent class Pointer Points to a subclass, if the method quilt class is overwritten, the method called is the subclass method. If the method is not overwritten, the method of the parent class is called. If the method of the parent class is not virtual and the subclass has the same method, the method of the parent class will not be overwritten, that is: the pointer of the parent class pointing to the subclass does not call the subclass method, but the method of the parent class. The destructor of the same parent class are not virtual, so when the delete pointer is used, unexpected situations will occur. constructors like this will not be called anyway, so when you decide to make a Class A parent class, set its destructor to virtual.

However, you do not need to make the destructor of each class virtual, because classes containing virtual methods have a pointer to virtual table, which will make the object larger, if the object is unlikely, the cost may double. Only when the class contains at least one virtual method can its destructor become virtual.

 

15: Let operator = return the reference of * This

Let's first look at an equation: (A = B) = C. To implement this equation, operator = certainly cannot return void. You can assign a value to * void, but you cannot assign a value to void, the Return Method of operator = cannot be by value. If so, (a = B) returns a copy of A or B (the correct method should be the reference of ), the value of C is assigned to this copy, but the values of A and B are not changed. This is of course not what we want. In order not to let this copy be generated, the returned value must be referenced. You can return the value using a pointer or reference. The pointer must add a * sign. Therefore, it is returned in reference mode, so whether to return the reference of a or the reference of B is undoubtedly a. If it is B, the execution order is as follows: first, assign B to, then, assign the value of C to B. Is the value assigned to the return value on the left actually! This is not what we want. What I want is to assign B to A and then assign C to A. Of course, writing such an equation is definitely not a qualified one.ProgramMember. Therefore, operator = must return the reference of the object on the left. I keep wondering why this is a pointer type rather than a reference? Because we must add return * this in the last sentence of operator = method; instead of return this;

 

16: assign values to all data member in operator =

There is no doubt that if some data member members are not assigned a value, will the assigned object be disabled! Sometimes we may forget to assign a value to the class when adding data member, or forget to assign a value to the data member of the parent class in operator = of the subclass. Of course, these are not the key points. Do not remember that they are not a problem. If an error occurs, you will naturally know. To assign a value to the data member of the parent class, you only need to add the base in the operator = of the subclass :: operator = (this); If the compiler does not support calling Base operator =, type conversion can be performed. static_case <& base> (* This) = derived; I found that in type conversion, the converted types are generally pointer or reference, especially when the conversion type is on the left, it must not be by value, and temporary variables will be generated, then assign values to the temporary variables, of course not what you want. When assigning values to data member of the pointer type, remember to assign values to the object referred to by the pointer instead of the pointer. If you want data member to point to the object, some addresses may be inaccessible and Memory leakage may occur.

 

17: In operator =, check whether you assign a value to yourself.

Before assigning a value to an object, you must first check whether the object dynamically configures the memory. If the memory is dynamically configured, you must first recycle the memory. Otherwise, when the object is assigned a value, when directed to another address, memory leakage will occur. Of course, you cannot remove it as soon as you find that it dynamically configures the memory, because you may assign yourself to yourself, you can't assign yourself a value to yourself after assigning yourself to yourself.

 

Valid C ++ series:

Objective C ++ constructor destructor assignment operator

Design and declaration of Objective C ++ classes and functions

Objective C ++ object-oriented and inheritance

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

 

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.