Inheritance of C + + learning (is a: assigning or initializing a base class object with a subclass object) __c++

Source: Internet
Author: User

1. The following example code shows the title:



2. Introduction is a from memory angle:

As shown in the following figure: When the base class contains two data members M_strname and M_iage, both public-owned and protected types will inherit the quilt class, and the subclass should also have his own data members, M_strcode and M_isalary, When we use the object of a subclass to assign a value to a base class object or to initialize an object of the base class, the essence of this is to assign the value of the data member inherited from the parent class to the object of the parent class, and the other data in the subclass object will be truncated, that is, lost.


If you point to a subclass object with the parent class pointer, the parent class pointer can access only the data members and member functions owned by the parent class, and you cannot access the data members and member functions unique to the subclass.



Based on the above memory structure, there is a problem ... As shown in the following illustration:


When you use a base class pointer to target a subclass of an object that is allocated from the heap, the following form a*p = new B; When the delete P;p=null is called; When the object is destroyed, is it called the destructor of parent class A or the constructor that calls subclass B? As the following illustration shows: The answer is to invoke the constructor of the parent class, so the problem is that the memory of those unique member variables that are not inherited from the parent class will not be freed and will result in a memory leak, which should prevent memory leaks

it. This introduces a new knowledge point: Virtual destructor function.

A virtual destructor is the addition of the virtual keyword before the destructor of the parent class, which is inherited, and the destructor of the subclass is also a virtual destructor, as in the following example:

Virtual ~person () {}//transforms a parent class's destructor into a virtual destructor

Virtual ~soldier () {The destructor of the}//subclass inherits this attribute and becomes a virtual destructor, even if subclasses do not write virtual, subclass constructors are also virtual destructors

Where a virtual destructor is used: When an inheritance relationship exists, the pointer to the parent class points to an object from a subclass allocated in the heap. Then you want to use the parent class pointer to release the memory, you will use the virtual destructor, after the virtual destructor, and then call the delete person, you will first call the subclass of the destructor, The constructor of the parent class is called again, as shown in the following illustration:



3. Analyzing the relationship of is a from the angle of function parameter

The first function, the argument is the object parent object, our argument can be the object of the parent class, or it can be the object of the subclass, the printed result shows that when instantiating the subclass, it invokes the constructor of the parent class and then calls its own constructor.

Because the parameter is the object of the parent class. When we call test1 to pass arguments to it, we will temporarily produce an object p, use it to invoke the play function, and after the function completes, p this temporary object will be destroyed, because the call two times test1, This is why the destructor of the parent class is invoked two times in the printed result. But note that the actual participant assigns the temporary object P, that is, the assignment between the objects, so the copy constructor of person is invoked as shown in the following figure:



If the same argument calls Test2 (person &p), the calling code is as follows: Test2 (P); test2 (s); the printed results are shown in the following illustration, and the destructor is not invoked, because when the argument is passed in as a meal, Just equivalent to an actual reference to an alias p, and does not produce a temporary object, so the function does not have to destructor when the temporary object, will not invoke ~person ();

If you call Test3 (person* p), the calling code is as follows: Test3 (&p); Test3 (&s); The results printed are the same as the call Test2 (person& p), and the destructor is not invoked because the pointer as a parameter Just let the pointer point to the address of the argument, so there is no temporary object, so the function does not call the destructor when it is done.

Because Test2 () and test3 () do not produce temporary variables with references and pointers, you do not have to invoke copy constructors and destructors, so execution is more efficient.


Add small points of knowledge: you distinguish it.

Prerequisite: Class B:public A

b b;

A A = b;//initializes the object of parent Class A with the object of subclass B

A A1;

A1 = b;//Assignment of the object of subclass B to the object of the parent class A

A*p = &b;//object B to subclass B with a pointer to the parent class A

A&A2 = b;//Initializes a reference to the parent class A with the object of subclass B


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.