More Effective C ++ Reading Notes (2)

Source: Internet
Author: User

1. Do not try to overload |, & operator, because they use the short-circuit Evaluate Method (once the true value of a Boolean expression is determined, even if some expressions are not tested, the Boolean expression also stops the operation), and the function call method is used after the overload. When a function is called, all its parameters need to be calculated. Therefore, when the function functions operator & operator | is called, both parameters need to be calculated. In other words, the short circuit calculation method is not used. Second, the C ++ language specification does not define the computing sequence of function parameters, so there is no way to know which of expression 1 and expression 2 is calculated first. It is entirely different from the short-circuit calculation method with the calculation sequence from the left parameter to the right parameter. Similarly, do not try to overload the comma (,) operator, because an expression containing a comma first calculates the expression on the left of the comma, and then calculates the expression on the right of the comma, the result of the entire expression is the value of the expression on the right of the comma, which may be different from the order in which parameters are calculated by the function call method. Because the important thing about the overload operator is that the behavior characteristics after the overload are the same as the expected (default behavior characteristics. Otherwise, overloading is totally rash. C ++ specifies operators that cannot be overloaded .,.*,::,? :, New, delete, sizeof, typeid, static_cast, dynamic_cast, const_cast, reinterpret_cast

2. Understand new and delete (new operators and operator new operations) with different meanings, and analyze a piece of code first:
String * ps = new string ("Memory Management ");
The new operator is used. This operator is just like sizeof which is built in a language. You cannot change its meaning and its functions are always the same.
The function to be completed is divided into two parts: the first part is to allocate enough memory to accommodate the required types of objects. The second part is that it calls the constructor to initialize objects in the memory. The new operator always does these two things. You cannot change its behavior in any way (you cannot reload it ).
The first part of the new operator is to allocate memory. The name of the called function is operator new (reload allowed ).
The operator new function is usually declared as follows:
Void * operator new (size_t size );
The return value type is void *, because this function returns an unprocessed (raw) pointer with uninitialized memory. The size_t parameter determines how much memory is allocated. You can add an extra parameter to overload the function operator new, but the first parameter type must be size_t. Operator new only allocates memory (returns a void * pointer, a bit similar to the C language malloc ). It knows nothing about constructors.
When your compiler encounters such a statement:
String * ps = new string ("Memory Management ");
The generated code is more or less similar to the following code (pseudo code:
Void * memory = operator new (sizeof (string); // gets unprocessed memory
Call string: string ("Memory Management") on * memory; // String object initialization
String * ps = static_cast <string *> (memory); // the object in the memory is a ps pointer pointing to a new object.

Similarly, we analyze and locate the new operator, for example:
New (buffer) Widget (widgetSize)
When the new operator implicitly calls the operator new function, the variable (memory address buffer) is passed to it. In addition to the mandatory size_t parameter, the called operator new function must also accept the void * pointer parameter, pointing to the memory space occupied by the constructor. This operator new is placement new, which looks like this:
Void * operator new (size_t, void * location)
{
Return location;
}
Summary:
To create an object on the stack, use the new operator. It allocates both memory and calls constructors for objects. If you only want to allocate memory, you should call the operator new function; it will not call the constructor. If you want to customize your memory allocation process when the heap object is created, you should write your own operator new function and then use the new operator, the new operator calls your customized operator new. If you want to create an object in a memory with obtained pointers, you should use placement new.

There is a similar conclusion for delete and operator delete:
Delete first calls the Destructor and then calls operator delete to release the memory. operator delete only releases the memory. If you use placement new to create objects in the memory, you should avoid using the delete operator in the memory. Because the delete operator calls operator delete to release memory, but the memory containing objects is not initially allocated by operator new, placement new is just a pointer to it. Who knows where the pointer comes from? Instead, you should explicitly call the object's destructor to relieve the constructor's influence.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/digu/archive/2007/11/13/1882023.aspx

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.