C ++ Object Memory Analysis 3

Source: Internet
Author: User

In section 2, we talk about the subclass inherited from the base class with virtual functions. There is a virtual function pointer at the front of the object. However, what if the base class does not have a virtual function and the Child class does? Read this section and think about the following questions: Are there virtual function pointers and virtual function tables for the base classes without virtual functions? Child classes inherited from this class, and how the elements in the memory are laid out.
Subject3: Child class inherited from the base class without virtual functions. we delete the two virtual functions of the cbasic class in the previous section. cfinal inherits from the cbasic class and adds the new virtual function avg. The class diagram is as follows:

ClassCbasic
{
Public:
Cbasic ()
{
Array =New Int[2];
}
IntI;
Int* Array;
VoidHelloworld ()
{
Cout <"Hello World" <Endl;
}
};ClassCfinal:PublicCbasic
{
Public:
VirtualIntAVG (IntA,IntB)
{
ReturnA + B/2;
}
IntIfinal;
};
Create the following object
Cbasic * B =NewCbasic;
Cfinal * f =NewCfinal;
Cbasic * b1 = (cbasic *) F;

We can still use the Watch window to observe the layout of elements in the object, but this chapter will not detail the analysis process. Let's look at the memory layout of the object in this case:


We found that cbasic class object B does not have a virtual function pointer, while cfinal Class Object F has a virtual function pointer at the frontend of the object address; in the cfinal Class Object Memory, The cbasic object begins with the address next to the virtual function pointer. This means that when the cfinal class object is converted to the cbasic type, only four bytes must be added to the address pointed to by the object pointer to point to the cbasic object correctly. Print the values of F and B1. We found that b1 = F + 4.
Conclusion This chapter is short. Let's draw a conclusion:
For the sub-classes that inherit from the base classes without virtual functions:
1) Its base class objects do not have virtual function pointers and virtual function tables.
2) the virtual function pointer of the subclass is located at the frontend of the object memory, and the base class object in the subclass begins at the position next to the virtual function pointer.
3) when the subclass object is converted to the base class typePointer Adjustment, The Object Pointer Points to the address plus four bytes, skip the position of the virtual function pointer; otherwise, when the base class object is converted to a subclass object, the pointer address is reduced by 4.
4) The member variables of the subclass object are stored at the end of the base class object.

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.