[Geekband] C + + Learning notes (2)--bigthree, OOP

Source: Internet
Author: User

This note is divided into two parts, the first part of the string class as an example of object-based programming, focusing on the construction and destruction, copy constructors, copy assignment function three important functions. This section, combined with the content in Notes (1), is the main content of object-based programming. In the second part, the object-oriented programming (OOP) learning based on object programming is mastered, and the combination, inheritance and delegation relationship between classes are explained.

The first part, taking the string class (with pointer Class) as an example, explains the key function "Big three"

1 classString2 {3  Public:                                 4String (Const Char* cstr=0); 5String (Conststring&str); 6string&operator=(Conststring&str); 7~String (); 8    Char* GET_C_STR ()Const{returnm_data;}9 Private:Ten    Char*m_data; One};
    1. Constructors and destructors

As described in note (1), a constructor is a special function that is called when an object is generated, and a destructor is a special function that is called at the end of a variable's life. For objects of the complex class, destructors do not require special operations; At this point, the compiler automatically provides a default destructor whose function body is empty.

  Why does the string class require special destructors?

First Look at what the string class did when it was created:

 inlinestring::string ( const  char  * CStr) { if   (CStr) {m_data  = new  char  [strlen      (CStr) +1   else   {m_data  = new  char  [1  ];  *m_data =  \0   '  

As you can see, when constructing an object of the string class, relying on pointers, the new method is used to request a space in the heap. If a special destructor is not used, the lifetime of the member pointer variable is ended, but the memory requested by the pointer variable is not returned, causing a memory overflow. Therefore, there must be a corresponding destructor. The operation of the destructor is simple and requires only delete [] m_data;

It should be noted that the constructor is to allocate the member memory and assign the initial value of the work before entering the function body, and the destructor is to enter the function body to perform operations, exit the function body after the compiler automatically return the memory of the member variable.

This example shows that when a new operation of the request heap space exists in the constructor, the delete operation must be performed at the time of the destructor.

2. Default copy constructor and default copy assignment function

A copy constructor is a function that is called when an object is used to initialize a homogeneous object.

For example:

1 String A; 2 String B (A); 3 String C = A;

The copy constructor is called when string objects B and C are generated. Its function is declared as

  


 The so-called copy assignment function is the function used to assign a value to another object using one object, that is, operator overloading of the ' = ' operator.

By default, the copy constructor and copy assignment function provided by C + + is a simple bit copy that copies the member variable one-by-one and all the data is exactly the same, saying that this copy is called shallow copy. For a pure data class, there is no need for the designer to provide copy constructors and copy assignment functions, but for classes with pointers, shallow copy has the problem:

After a shallow copy of the a=b, the address of the pointer is bit copied, it becomes

In this case, the "world\0" string under the heap space will never be deleted, resulting in a memory leak. For copy construction operations (that is, string B = a), bit replication does not allocate a memory to B, but it causes the "Hello\0" string to be pointed to by two pointers at the same time, and when one of the destructors is made, the area pointed to by the pointer is deleted, and the remaining pointer becomes the wild pointer. A memory overflow occurs when another destructor occurs, and the delete operation is performed on this wild pointer.

The core of these problems is the shallow copy, which translates the shallow copy into a deep copy, that is, the pointer in B is allocated a heap of memory when copying, and then the corresponding string of the A object is written in this heap memory.

  

3.

[Geekband] C + + Learning notes (2)--bigthree, OOP

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.