Special C ++ tools and technologies

Source: Internet
Author: User

Special tools and technologies

 

I. Optimized memory

1. Policy: Pre-allocate the memory used to create objects. When necessary, create new objects for each user in the pre-allocated memory.
2. allocator class; A. Destroy (P), run the destructor of the object referred to by the T * pointer p. Note: running the Destructor does not release the memory of the object. To release the memory, call function. deallocate (p, n). Release n t * objects of the T type in the address contained in the T * pointer of P.
3. Operator new and operator Delete functions. Note the differences between operator new and delete expressions. Operator new and operator Delete are functions, while new and delete are expressions. They all return void * pointers instead of typed pointers. It is safer to use Allocator than to directly use operator new and operator Delete functions. Standard library functions operator new and operator Delete are low-level versions of allocate and deallocate members of allocator. They are allocated but do not initialize memory. Note: Call Operator
The delete function does not run the destructor. It only releases the specified memory.
4. Locate the new expression. Locate the new expression to initialize an object in the allocated original memory. Unlike other versions of new, the new expression does not allocate memory. Instead, it accepts pointers pointing to allocated but not constructed memory and initializes an object in the memory. Positioning the new expression enables us to construct a new object at a specific pre-allocated memory address. The expression is new (place_address) type or new (place_address) type (initialzer-list). place_address must be a pointer and initial-list is an empty initialization list, for example: New
(SP) string (B, E ). When positioning a new expression to initialize an object, it can use any constructor and directly create an object.
5. When creating a new object, you can construct the object in the pre-allocated space. When releasing objects, place them back in the blocks of pre-allocated objects instead of returning the memory to the system. Therefore, you can manage your own memory by defining (or inheriting) Your Own members named operator new and operator Delete. Otherwise, you can call the standard library version of these functions. Before constructing an object, use operator new to use operator Delete after the object is revoked. Therefore, the new and delete functions must be static.
Ii. runtime type recognition 1. Through runtime type recognition (rtti), the program can use pointers or references of the base class to retrieve these pointers or reference the actual derived types of the objects. The rtti: * typeid operator is provided using two operators to return a pointer or reference the actual type of the object referred to * dynamic_cast operator, securely convert a pointer or reference of a base class to a pointer or reference of a derived type
2. dynamic_cast operator; usage: dynamic_cast <type */&> (VAL). type is the target type of conversion, and Val is the object of the base class type. For example, if (derived * derivedptr = dynamic_cast <derived *> (baseptr ){...}.
Verification of the dynamic_cast operator must be performed at runtime. If the conversion to the pointer type fails, the result of dynamic_cast is 0. If the conversion to the reference type dynamic_cast fails, a bad_cast type exception is thrown.
3. typeid operator; expression: typeid (e). E is an arbitrary expression or type name. The typeid operator can be used with any type of expression. The most common purpose of typeid is to compare the types of two expressions, or compare the types of expressions with specific types. For example, if (typeif (* derivedptr) = typeid (derived );

3. class member pointers 1. Declare member pointers. The member pointer is only applicable to non-static members. The static member pointer is a common pointer.
2. Define the pointer of a data member. Both the member type and the class to which the member belongs must be indicated. For example, string screen: * Indicates a pointer to a string Member of the screen class.
3. Define the pointer of a member function. Consistent matching requirements: * type and number of function parameters, including whether the Member is const * return type * type of the class to which the member belongs, for example, char (screen: *) () const
4. Use the pointer of a class member. * The member pointer resolves the reference operator (. *) Get a member from an object or reference * member pointer arrow operator (-> *) Get a member through the object pointer and define an array to save the pointer pointing to each cursor moving function.
For more information, see the previous article: pointers to class members are not pointers. Or refer to "C ++ must know".
4. Nested classes define another class within one class. Nested classes are independent classes, which are basically irrelevant to their peripheral classes. Therefore, the peripheral classes and Nested classes are independent of each other. The peripheral class has special access to the members of the nested class, And the nested class has no special access to the members of its peripheral class. The name of the nested class is visible in the scope of its peripheral class, but not in the scope of other class scopes or defined peripheral classes. Nested classes can have members of the same type as non-nested classes.
1. The class nested inside the class template is the template;
2. Definition; members of the nested class defined outside the class cannot be defined inside the peripheral class, and members of the nested class are not members of the peripheral class. To define the class body outside the peripheral class, you must use the name of the peripheral class to specify the name of the nested class. Note that the nested class must still be declared in the definition body of the peripheral class. The definition of static members declared in the nested class is also in the outer scope.
3. nested type objects only contain members of the nested type. You cannot use this to obtain members of the peripheral class. The peripheral class cannot directly call Members in the nested class. Nested classes can directly reference static members, type names, and enumeration members of peripheral classes.
5. Union: space-saving class. 1. A union object can have multiple data members, but only one member can have a value at any time.
2. The volume of storage allocated to the Union object must be at least as large as the biggest annoying data member of the Union object.
3. Union cannot be used as a base class, the member can be a virtual function, or a class member that defines constructors, destructor, or value assignment operators.
4. To know the values in the Union object, an enumerated variable is usually used as the discriminant of the Union object.


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.