C + +, the size of class

Source: Internet
Author: User

Does not allocate space for functions of classes and objects;

In a class where virtual functions are declared in virtual, a pointer is hidden that points to the virtual function table, which is the same for pure virtual functions;

For virtual inheritance, there is also a pointer to the parent class, which is a pointer to the virtual base class (Pointer to Virtual base class).

#include"stdafx.h"#include<iostream>using namespacestd; #include"T1.h"classtnull{};classti{inti;};classtic{inti; Charc;};classtf{voidFvoid){}};classt_vf{Virtual voidFvoid){}};classTif { Public:    void Set(intX) {i =x;}Private:    inti;};classTI_VF { Public:    Virtual void Set(intX) {i =x;}Private:    inti;};classti_v0f { Public:    Virtual void Set(intx) =0;Private:    inti;};classTI_SF {inti; Static voidsfvoid){}};classtt{TT () {}};classtxt{~TxT () {}};classttt{TTT () {}~TTT () {}Private: Tif*m_ptif;};classtttfff{ Public: Tttfff () {m_ptif=NewTif; M_ptif-Set(0); }    ~tttfff () {delete (m_ptif); M_ptif=0; }    voidF1 (void){}    voidF2 (void){}    voidF3 (void){}Private: Tif*m_ptif;};classttt_vfff{ Public: Ttt_vfff () {m_ptif=NewTif; M_ptif-Set(0); }    ~ttt_vfff () {delete (m_ptif); M_ptif=0; }    Virtual voidF1 (void){}    Virtual voidF2 (void){}    Virtual voidF3 (void){}Private: Tif*m_ptif;};voidT1 (void) {cout<<"sizeof (char) ="<<sizeof(Char) <<Endl; cout<<"sizeof (class tnull) ="<<sizeof(classTnull) <<Endl; cout<<"sizeof (class Ti) ="<<sizeof(classTi) <<Endl; cout<<"sizeof (class Tic) ="<<sizeof(classTic) <<Endl; cout<<"sizeof (class Tf) ="<<sizeof(classTF) <<Endl; cout<<"sizeof (class Tif) ="<<sizeof(classTIF) <<Endl; cout<<"sizeof (class T_VF) ="<<sizeof(classT_VF) <<Endl; cout<<"sizeof (class TI_VF) ="<<sizeof(classTI_VF) <<Endl; cout<<"sizeof (class ti_v0f) ="<<sizeof(classti_v0f) <<Endl; cout<<"sizeof (class ti_sf) ="<<sizeof(classTI_SF) <<Endl; cout<<"sizeof (class TT) ="<<sizeof(classTT) <<Endl; cout<<"sizeof (class TxT) ="<<sizeof(classTxT) <<Endl; cout<<"sizeof (class TTT) ="<<sizeof(classTTT) <<Endl; cout<<"sizeof (class tttfff) ="<<sizeof(classTTTFFF) <<Endl; cout<<"sizeof (class ttt_vfff) ="<<sizeof(classTTT_VFFF) <<Endl;    TTTFFF T; cout<<"sizeof (tttfff t) ="<<sizeof(t) <<endl;/*sizeof (tttfff t) = 4*/}/*sizeof (char) = 1sizeof (class tnull) = 1sizeof (class Ti) = 4sizeof (class Tic) = 8sizeof (class Tf) = 1sizeof (class Tif) = 4sizeof (class T_VF) = 4sizeof (class TI_VF) = 8sizeof (class ti_v0f) = 8sizeof (class ti_sf) = 4sizeof (class TT) = 1sizeof ( Class TxT) = 1sizeof (class TTT) = 4sizeof (class tttfff) = 4sizeof (class ttt_vfff) = 8sizeof (tttfff t) = 4 Press any key to continue ...*/

Extension: 1. C + + class size http://www.cnblogs.com/luxiaoxun/archive/2012/09/01/2666395.html

2. C + + virtual function table parsing http://blog.csdn.net/haoel/article/details/1948051

C + +, the size of class

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.