- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
条款14确定基类有虚析构函数(Clause 14 make sure the base class has virtual destructor)
条款14确定基类有虚析构函数(Clause 14 make sure the base class has virtual destructor)
Clause 14: determines that the base class has a virtual destructor
Sometimes a class wants to keep track of how many objects exist. A simple way is to create a static class member to count the number of objects. This member is initialized to 0, plus 1 in the constructor, and 1 in the destructor. (clause M26 illustrates how to encapsulate this method so that it can be easily added to any class. My article on counting objects provides some additional improvements to this technology.
Suppose in a military application there is a class that represents the enemys target:
Class enemytarget {
Public:
Enemytarget () {++numtargets;}
Enemytarget (const, enemytarget) {++numtargets;}
~enemytarget () {--numtargets;}
Static, size_t, numberoftargets ()
{return numtargets}
Virtual bool destroy (enemytarget); / / destroy objects
/ / return success
Private:
Static size_t numtargets; / / object counter
};
/ / static members of the class should be defined outside the class;
/ / default initialized to 0
Size_t enemytarget:: numtargets;
This class wont win a government defense contract for you. Its too far from the Defense Departments requirements, but its enough to meet the needs of our problems here.
Enemy tanks are a special enemy target, so its natural to think of it as a class that is derived from enemytarget in public inheritance (see clauses 35 and M33). Because not only should we care about the total number of enemy targets, but also the total number of enemy tanks, so we use the same techniques mentioned in the derived class as in the base class:
Class, enemytank:, public, enemytarget {
Public:
Enemytank () {++numtanks;}
Enemytank (const, enemytank, RHS)
: enemytarget (RHS)
{++numtanks;}
~enemytank () {--numtanks;}
Static, size_t, numberoftanks ()
{return numtanks}
Virtual, bool, destroy ();
Private:
Static size_t numtanks; / / object counter tank
};
After youve written the code for the two classes, youll b
您可能关注的文档
- 女人魅力(Feminine charm).doc
- 女孩心事(Girl worry).doc
- 女怕嫁错郎,男怕进北航!!!(Women are afraid to marry the wrong husband, male fear in buaa!!!).doc
- 女包品牌(Brand handbags).doc
- 女性户外常识(Female outdoor knowledge).doc
- 女孩取名(Girl named).doc
- 女孩要懂得常识(Girls need common sense).doc
- 奇妙的爬行(Wonderful crawl).doc
- 女生寝室卧谈话题大暴光(The girls dormitory talk topic big exposure).doc
- 女老板体验民俗手工坊中寻找财富(Female boss to experience folk custom handicraft shop looking for wealth).doc
- 松木坪中学2005年春教学工作计划(Pine flat middle school spring 2005 teaching work plan).doc
- 林书豪采访(Jeremy Lin interview).doc
- 林则徐的流放生活(Lin Zexu's exile).doc
- 林明龙三大计划促汕头产业升级管理(Lin Minglong three plans to promote the management of industrial upgrading in Shantou).doc
- 林宥嘉《感官世界》(Yoga Lin's the world of senses).doc
- 染色(dyeing).doc
- 柳传志的新时代(The new age of Liu Chuanzhi).doc
- 柳荣高绩效生产管理与成本控制(Liu Ronggao performance, production management and cost control).doc
- 树立大学生正确的网络伦理观(Establishing the correct network ethics of College Students).doc
- 桐乡崇德变(Tongxiang Chongde variable).doc
文档评论(0)