C++ 空类

 

所在的环境是VS2008下的

本文中空类的定义是:没有数据成员,没有函数的类.(不包括默认的)

1、空类或者空类对象的大小(无继承关系)

 

输出结果:

the Animal Class space = 1

the bird space = 1

分析:

分配1字节,以避免不同对象指向同一个地址。具体为什么是1字节,我也没真正的清楚,我认为应该是空类对象的地址与该字 节的地址一致,该字节应该是编译器添加的。后续看了对象的地址就可能会是这样认识的。

补充:

http://www.vckbase.com/bbs/prime/viewprime.asp?id=525有这样的描述:(空类)可以作为一个要定义但还没有定义 的类的占位符。例如,它可以被用作临时性的接口类,作为其它类的基础;不用等待全部实现都完成。空类还能被用于在类之间强加派 生关系,这种关系最初没有从基类祖先传下来,这被称为自底向上设计。最后,它还可以被用于为哑变量创建类型以便区分函数不同的 重载版本。事实上,操作符new就是像这样重载。

2、空类或者空类对象的大小(有继承关系)

为了不增加讨论的复杂性,这里继承的类也是空类。也假设您已经清楚了继承、虚继承的概念。

1)  继承

 

输出结果

the Animal Class space = 1

the bird space = 1

分析:

继承的基类都是空类,大小是1,不管有多少个父类。

     2)虚继承

 

  输出结果:

the Animal Class space = 8

the bird space = 8

分析:

对每一个虚继承基类,都有一个虚基类索引(或者偏移量、指针),占4字节。猜测这时空类对象的指针应该与某个虚基类索 引相等。

3)继承和虚继承混合

 

输出结果:

the Animal Class space = 4

the bird space = 4

分析:

在有虚继承的话,大小为虚继承的大小,不计算继承的空类的大小。猜测当前空类对象的地址应该与某个虚继承的基类的索 引相等。

3、总结

VS2008环境下

空类:没有数据成员,没有函数的类。(不包括默认的)

1) 无继承关系的空类大小为1

2) 有虚继承的,对每一个虚继承基类,都有一个虚基类索引,占4字节;

3) 空类继承多个空类,大小仍然是1

4) 空类虚继承N个空类,大小为N*4

5) 空类虚继承N个空类,继承M个空类,大小是N*4;

6) 我认为空类的区分是默认的1字节的地址或者虚继承中的某个虚基类索引的地址,两者只取其一即可,也就是说空类对象的地址应该是与这两个中的一个相等的。

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ZSCPascal AppWizard has created this ZSCPascal application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ZSCPascal application. ZSCPascal.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CZSCPascalApp application class. ZSCPascal.cpp This is the main application source file that contains the application class CZSCPascalApp. ZSCPascal.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Developer Studio. resSCPascal.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file ZSCPascal.rc. resSCPascal.rc2 This file contains resources that are not edited by Microsoft Developer Studio. You should place all resources not editable by the resource editor in this file. ZSCPascal.reg This is an example .REG file that shows you the kind of registration settings the framework will set for you. You can use this as a .REG file to go along with your application or just delete it and rely on the default RegisterShellFileTypes registration. ZSCPascal.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CMDIFrameWnd and controls all MDI frame features. resToolbar.bmp This bitmap file is used to create tiled images for the toolbar. The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap along with the array in MainFrm.cpp to add more toolbar buttons. AppWizard creates one document type and one view: ZSCPascalDoc.h, ZSCPascalDoc.cpp - the document These files contain your CZSCPascalDoc class. Edit these files to add your special document data and to implement file saving and loading (via CZSCPascalDoc::Serialize). ZSCPascalView.h, ZSCPascalView.cpp - the view of the document These files contain your CZSCPascalView class. CZSCPascalView objects are used to view CZSCPascalDoc objects. resSCPascalDoc.ico This is an icon file, which is used as the icon for MDI child windows for the CZSCPascalDoc class. This icon is included by the main resource file ZSCPascal.rc. Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ZSCPascal.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Developer Studio reads and updates this file. Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system s current language, you will need to copy the corresponding localized resources MFC40XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC40DEU.DLL contains resources translated to German.) If you don t do this, some of the UI elements of your application will remain in the language of the operating system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值