C + + empty class size

Source: Internet
Author: User

Class A {};
Class b{};
Class C:public a{
virtual void fun () = 0;
};
Class D:public B,public c{};

Class A, B is obviously empty class, its size should be 0, why the compiler output is 1? This is the reason we have just described the instantiation (empty class can also be instantiated), each instance has a unique address in memory, in order to achieve this, the compiler will often give an empty class implicitly add a byte, so that the empty class in memory after instantiation has a unique address. So a, B has a size of 1.

and Class C is derived from the Class A, it has a pure virtual function, because of the reason for the virtual function, there is a pointer to the virtual function (VPTR), the 32-bit system is allocated to the size of the pointer is 4 bytes, so finally the size of Class C is 4.

C + + empty class size

Related Article

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.