Memory relationships between Child class objects and parent class objects in C ++

Source: Internet
Author: User

 

Question: for example, question. Assume that there are parent class A and Child class B.

A instantiate an object a B instantiate an object B

What is the address relationship between A and B in the memory. Are their first addresses the same?

Answer:

They do not have. Just like generating two other types of variables. Of course, the object B you instantiate is related to. The four bytes starting with object B are the virtual function table, followed by the data member of the base class, and then the data member of the subclass.
 
 
Peer: Where is the class object in C ++ in memory after it is instantiated? Heap or stack? I (thinking, but not sure): either in the stack or in the stack: under what circumstances, in the stack? Me: if the class object instance appears as a local variable in the stack, the scope ends, and the memory occupied by the class instance is released, the new class will be created in the heap in the form of a pointer, you must manually delete the file in the heap. Peer: Who controls stack allocation and recycling? Me: Operating System peer: What are the destructor of the class instance? Me (a little dizzy)

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.