Sizeof of a class should be considered

Source: Internet
Author: User

During the interview yesterday, the interviewer asked me to talk about the things that should be taken into consideration in a sizeof class. I thought, that's great. Previously, sizeof did a lot of work, the non-static and non-Global members of the class are mentioned. If there is a virtual function, a pointer to the virtual table should be added, which is 4 bytes in size. Then, this is probably the case. The interviewer asked, What is the implementation mechanism of virtual functions? After talking about something like a virtual table, he asked, is sizeof the same if it is a virtual inheritance? First, I thought to myself, mom. What is virtual inheritance, and then I just returned it calmly? The same should be true. We all use a pointer to point to the continuous table, the interviewer did not say anything.

After the interview, I never thought about sizeof again. I always felt that there was no problem. But this early morning, I thought about it. I searched the internet and found that it was not as simple as I thought, the more I read it, the more confused I was. I simply tried to write several classes on VC. The results were really different. Yesterday's interview was really sad. t_t  Let's summarize what we just learned:1. Empty Class { }; The result of sizeof is 1, because even if there is no Member or the like, a class must at least provide a space for it, otherwise there will be no meaning.Ii. Simple Class { Int; Virtual fun ();} This is also a good result, that is, sizeof (A. A) + 4 (pointer to the virtual table)Iii. Normal subclass inheritance. The parent class does not include the virtual function class. A{ Int;} Class B: Public { Int B; Virtual fun ();} Sizeof (B) = sizeof (A) + sizeof (B. B) + 4 (pointing to A virtual table pointer) 4. Subclass inheritance, parent class containing virtual function class A{ Int; Virtual fun1 ();} Class B: public { Int B; Virtual fun ();} Sizeof (B) = sizeof (A)-4 (sizeof (A) has A pointer to the virtual table) + sizeof (B. b) + 4 (pointing to the virtual table pointer) because of normal inheritance, the virtual functions of sub-classes and parent classes are stored in the same virtual table, you only need to save a pointer to the continued table. 5. Virtual inheritance of sub-classes, parent class without virtual function class A{ Int; } Class B: virtual public { Int B; Virtual fun ();} Sizeof (B) = sizeof (A) + 4 (indicating the starting offset of the parent class storage space) + sizeof (B. b) + 4 (pointer to the virtual table of B) 6. Subclass virtual inheritance, parent class containing virtual function class A{ Int; Virtual fun1 (); } Class B: virtual public { Int B; Virtual fun ();} Sizeof (B) = sizeof (A) + 4 (indicating the starting offset of the parent class storage space) + sizeof (B. b) + 4 (pointer to the virtual table of B) when the virtual inheritance is performed, the virtual function tables of the parent class and the subclass are open separately. Therefore, the two pointers pointing to the corresponding table are respectively stored, therefore, you do not need to subtract the size of the continued table pointer in sizeof (. The sizeof alignment problems I have read before are too basic. The two sides are really deep, and the preparation is still inadequate. I can't help but pray...Address: http://blog.sina.com.cn/s/blog_728161840100u2ib.html

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.