Class size-sizeof (2. virtual function inheritance)

Source: Internet
Author: User

PreviousArticleAfter studying the four rules about the class size, we can combine the virtual function table to study the class size.

 

Class base <br/>{< br/> Public: <br/> base () {}; <br/> virtual ~ Base () {}; <br/> void set_num (INT num) <br/>{< br/> A = num; <br/>}< br/> virtual int get_num () <br/>{< br/> return a; <br/>}< br/> PRIVATE: <br/> int A; <br/> char * P; <br/>}; </P> <p> class derive: public base <br/>{< br/> Public: <br/> derive (): Base () {}; <br/> ~ Derive () {}; <br/> virtual int get_num () <br/>{< br/> return D; <br/>}< br/> PRIVATE: <br/> static int st; <br/> int D; <br/> char * P; <br/> char C; </P> <p> }; </P> <p> int main () <br/>{< br/> cout <sizeof (base) <Endl; <br/> cout <sizeof (derive) <Endl; <br/> return 0; <br/>}

 

The virtual int get_num () function is added to the base class, And the subclass implements the virtual int get_num () function again.

But the result is still

12

24

 

It indicates that the subclass only shares the virtual function table of the parent class. Therefore, once the parent class has a virtual function, the virtual function of the subclass is not included in the sizeof size.

 

This can be considered as a supplementary rule.

 

 

 

 

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.