建筑效果图网站有哪些,创业投资平台,dw制作电商网页,深圳移动端网站建设模板C 对象模型 C Object Model 文章目录 C 对象模型 C Object ModelC语言的数据及函数C的类C对象模型 C语言的数据及函数
C语言中#xff0c;数据和函数是分开声明的。
数据
typedef struct point2d
{float x;float y;
} Point2d;函数
打印Point2d的数值
void Point2d_print…C 对象模型 C Object Model 文章目录 C 对象模型 C Object ModelC语言的数据及函数C的类C对象模型 C语言的数据及函数
C语言中数据和函数是分开声明的。
数据
typedef struct point2d
{float x;float y;
} Point2d;函数
打印Point2d的数值
void Point2d_print(const Point2d * pd)
{printf((%f, %f), pd-x, pd-y);
}C的类
同样实现C语言的功能C中可以进行如下定义
class Point2d
{Point2d(float x, float y):m_x(x), m_y(y){}print(){printf((%f, %f), m_x, m_y);}private:float m_x;float m_y;
};问题Point2d从C到C内存布局上有啥变化呢
答案是没有变化。 本例中C 中的数据成员和C中一样不一样的是C中多了一个非静态成员函数print但非静态成员函数与数据成员是分离的并不占用类的大小。也就是说print函数的调用实际为print(Point2d* pd)就如同C语言的Point2d_print调用。
C内存布局的额外负担有virtual引起主要包好虚函数(virtual function)和虚基类(virtual base class)。
C对象模型
C中 类数据成员有两种静态(static) 和 非静态(nonstatic) 类成员函数有三种静态函数(static)、非静态函数(nonstatic) 和 虚函数(virtual)
C对象模型
非静态(nonstatic)数据成员在类内部静态(static)在类外部静态函数(static)和非静态函数(nonstatic)在类外部虚函数(virtual) a. 每个类产生一堆指向虚函数的指针放在虚表(virtual table, vtbl) b. 每个类对象添加一个指向虚表的指针(vptr)。 c. 每个类的type_info(用于支持RTTI)也由虚表指定一般位于第一个slot处。
class Point2d
{Point2d(float x, float y):m_x(x), m_y(y),m_pointCount(2){}virtual ~Point2d(){}static int getPointCount(){return m_pointCount;}print(){printf((%f, %f), m_x, m_y);}private:float m_x;float m_y;static int m_pointCount;
};上述代码的C对象模型如下
---------------
| float m_x; | virtual table
---------------
| float m_y; | ---------------
--------------- | | --------------------
| vptr Point2d ---------- ------ type_info(RTTI |
--------------- --------------- --------------------| | --------------------Point2d pt2; | ------ Point2d::~Point2d()|--------------- --------------------------------------------------------- ------------------------------------
| static int Point2d::m_pointCount | | Point2d::Point2d(float x, float y) |
------------------------------------- ------------------------------------------------------------------------- ------------------------------------
| static int Point2d::getPointCount() | | Point2d::print() |
------------------------------------- ------------------------------------ Reference:
深度探索C对象模型 文章转载自: http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.pbknh.cn.gov.cn.pbknh.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.dztp.cn.gov.cn.dztp.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.chrbp.cn.gov.cn.chrbp.cn