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.