C ++ Chapter 12

Chapter 12, classes and dynamic memory allocation

12.1 Dynamic Memory and class

   12.1.1 review examples and static class members

        1, can not initialize static member variables to traverse in the class declaration, because the statement describes how to allocate memory, but does not allocate memory. While for static class members can be initialized using a separate statement outside the class declaration. But if the static member is integral or enumeration type const, you can initialize in the class declaration.

        2, when used to allocate memory in the new constructor, you must delete the corresponding destructor to free memory. If new new [] (include brackets) to allocate memory, use Delete [] (including bracket) to free memory.

        3, note that the following code:
      StringBad Sailor = Sports; equivalent to:
           StringBad Sailor = StringBad (Sports); thus the corresponding constructor prototype should be as follows:
           StringBad (StringBad const &);
           That is, when you use an object to initialize another when an object, the compiler will automatically generate the constructor (called the copy constructor because it creates a copy of the object).

   12.1.2 implicit member function

        1, C ++ automatically provides these member functions:

l default constructor, if not defined constructor.

l copy constructor, if not defined.

l copy operator, if not defined.

l default destructor, if not defined.

l address operator, if not defined

More precisely, the compiler will generate the last four defined functions. If an object is assigned to another object, the compiler will provide definitions of the assignment operator.

         2, copy constructor

             1, the copy constructor is used to copy an object to a new object is created. That is, it is used to initialize the process, instead of the normal replication process. Prototype is as follows:
        class_name (class_name const &);

             2, when to call the copy constructor, the following four statements will be called:

l  StringBad ditto(motto);

l = StringBad Metoo motto;

l  StringBad also = StringBad(motto);

L * StringBad pStringBad = new StringBad (keyword);

Wherein the two intermediate statements may be used directly to create copy function configuration station and metoo Also, also be willing to use the copy constructor creates a temporary object, the object is assigned to the content of the temporary and metoo also. The last statement using the motto initialize an anonymous object and assign the address of the new object to pstring pointer.
Whenever the program generates a copy of the object, the compiler will use the copy constructor. Specifically, when the transfer function or the function returns an object by value object will use the copy constructor.    

3, replication configuration station function

l The default copy constructor will be a copy of a non-static members (members of the replication has become shallow copy), copy the value of membership.

l Note that the default is copied by value. This means that if two pointer by value assigned worth, then pointing to the same memory location. So be sure to note that memory location will not be released, and then was one of the recall. To avoid, you can use explicit stop function copy configuration to solve this problem. You can achieve the purpose of the depth of replication.

            4, the assignment operator

l C ++ class objects allows assignment, this is achieved by the automatic class assignment operator in red. This prototype operator as follows:
 class_name & class_name :: operator = (const & class_name);
it accepts and returns a pointer to the class object.

When l assigned to the existing object to another object, using the assignment operator overloaded.

L implicitly implement the assignment operator also be a member of a copy. If a member is itself a class object, the program will use the class definition for this assignment operator to copy the member, but does not affect static data members.

l is a shallow copy, it may lead to different objects point to the same address. To avoid, we should use the assignment operator is defined. The purpose of the depth of replication.

1, talking about new and delete

In the new address by the constructor, destructor delete by

2, in the following cases will be called destructor

When l If the object is a dynamic variable, the definition of the object block is completed when executed, it invokes the destructor of the object.

l If the object is a static variable (external, static, static, or from an external namespace), at the end of the program calls the destructor object.

l If the object is created with new, only if you explicitly using delete an object is deleted, its destructor will be called.

Guess you like

Origin www.cnblogs.com/yrz001030/p/12370208.html