当前位置: 首页 > news >正文

临沂免费做网站站长工具大全

临沂免费做网站,站长工具大全,台州做网站公司,游戏网站建设项目规划C中只能有一个实例的单例类 前面讨论的 President 类很不错,但存在一个缺陷:无法禁止通过实例化多个对象来创建多名总统: President One, Two, Three; 由于复制构造函数是私有的,其中每个对象都是不可复制的,但您的目…

C++中只能有一个实例的单例类

前面讨论的 President 类很不错,但存在一个缺陷:无法禁止通过实例化多个对象来创建多名总统:
President One, Two, Three;
由于复制构造函数是私有的,其中每个对象都是不可复制的,但您的目标是确保 President 类有且只有一个化身,即有了一个 President 对象后,就禁止创建其他的 President 对象。要实现这种功能强大的模式,可使用单例的概念,它使用私有构造函数、私有赋值运算符和静态实例成员。

提示:

将关键字 static 用于类的数据成员时,该数据成员将在所有实例之间共享。
将 static 用于函数中声明的局部变量时,该变量的值将在两次调用之间保持不变。
将 static 用于成员函数(方法)时,该方法将在所有成员之间共享。

要创建单例类,关键字 static 必不可少,如以下示例程序所示:

#include <iostream>
#include <string>
using namespace std;class President
{private:President() {}; // private default constructorPresident(const President&); // private copy constructorconst President& operator=(const President&); // assignment operatorstring name;public:static President& GetInstance(){// static objects are constructed only oncestatic President onlyInstance; return onlyInstance;}string GetName(){ return name; }void SetName(string InputName){ name = InputName; }
};int main()
{President& onlyPresident = President::GetInstance();onlyPresident.SetName("Abraham Lincoln");// uncomment lines to see how compile failures prohibit duplicates// President second; // cannot access constructor// President* third= new President(); // cannot access constructor// President fourth = onlyPresident; // cannot access copy constructor// onlyPresident = President::GetInstance(); // cannot access operator=cout << "The name of the President is: ";cout << President::GetInstance().GetName() << endl;return 0;
}

输出:

The name of the President is: Abraham Lincoln

分析:

第 28~43 行的 main( )包含大量注释,演示了各种创建 President 实例和拷贝的方式,它们都无法
通过编译。下面逐一进行分析。
34: // President second; // cannot access constructor
35: // President* third= new President(); // cannot access constructor
第 34 和 35 行分别试图使用默认构造函数在堆和自由存储区中创建对象, 但默认构造函数不可用,因为它是私有的,如第 7 行所示。
36: // President fourth = onlyPresident; // cannot access copy constructor
第 36 行试图使用复制构造函数创建现有对象的拷贝(在创建对象的同时赋值将调用复制构造函数),但在 main( )中不能使用复制构造函数,因为第 8 行将其声明成了私有的。
37: // OnlyPresident = President::GetInstance(); // cannot access operator=
第 37 行试图通过赋值创建对象的拷贝,但行不通,因为第 9 行将赋值运算符声明成了私有的。因此, 在main( )中, 不能创建President类的实例, 唯一的方法是使用静态函数GetInstance( )来获取President的实例,如第 30 行所示。 GetInstance( )是静态成员,类似于全局函数,无需通过对象来调用它。GetInstance( )是在第 14~19 行实现的, 它使用静态变量 onlyInstance 确保有且只有一个 President 实例。
为更好地理解这一点,可以认为第 17 行只执行一次(静态初始化),因此 GetInstance( )返回唯一一个President 实例,而不管您如何频繁地调用 President:: GetInstance( )。

该文章会更新,欢迎大家批评指正。

推荐一个零声学院的C++服务器开发课程,个人觉得老师讲得不错,
分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容
点击立即学习:C/C++后台高级服务器课程


文章转载自:
http://ninnyhammer.Lnnc.cn
http://occasion.Lnnc.cn
http://pyongyang.Lnnc.cn
http://swazzle.Lnnc.cn
http://flagon.Lnnc.cn
http://vinegrower.Lnnc.cn
http://trinomial.Lnnc.cn
http://conceptive.Lnnc.cn
http://hydroairplane.Lnnc.cn
http://brahminism.Lnnc.cn
http://dunstaple.Lnnc.cn
http://ikebana.Lnnc.cn
http://arisen.Lnnc.cn
http://pony.Lnnc.cn
http://courtesy.Lnnc.cn
http://blasphemer.Lnnc.cn
http://andaman.Lnnc.cn
http://toltec.Lnnc.cn
http://proteide.Lnnc.cn
http://depend.Lnnc.cn
http://hyporchema.Lnnc.cn
http://hussy.Lnnc.cn
http://crossbill.Lnnc.cn
http://quatrefoil.Lnnc.cn
http://unaffectionate.Lnnc.cn
http://epimerase.Lnnc.cn
http://volcanological.Lnnc.cn
http://rurigenous.Lnnc.cn
http://hemorrhage.Lnnc.cn
http://gnu.Lnnc.cn
http://psychoprison.Lnnc.cn
http://immunogenic.Lnnc.cn
http://catfoot.Lnnc.cn
http://watchmaking.Lnnc.cn
http://buttocks.Lnnc.cn
http://vindictive.Lnnc.cn
http://blackhearted.Lnnc.cn
http://rancidness.Lnnc.cn
http://endostyle.Lnnc.cn
http://pretonic.Lnnc.cn
http://bywork.Lnnc.cn
http://bibliophilist.Lnnc.cn
http://vomitous.Lnnc.cn
http://contentedly.Lnnc.cn
http://undiscoverable.Lnnc.cn
http://axiological.Lnnc.cn
http://whirlaway.Lnnc.cn
http://collaret.Lnnc.cn
http://haemolymph.Lnnc.cn
http://antalkali.Lnnc.cn
http://retirement.Lnnc.cn
http://mutsuhito.Lnnc.cn
http://revascularization.Lnnc.cn
http://antistrophic.Lnnc.cn
http://thrust.Lnnc.cn
http://nonprotein.Lnnc.cn
http://advolution.Lnnc.cn
http://rhetorical.Lnnc.cn
http://riboflavin.Lnnc.cn
http://blockship.Lnnc.cn
http://orangeism.Lnnc.cn
http://electrotherapeutical.Lnnc.cn
http://pandean.Lnnc.cn
http://bfa.Lnnc.cn
http://arrisways.Lnnc.cn
http://asphaltum.Lnnc.cn
http://cushaw.Lnnc.cn
http://mythological.Lnnc.cn
http://hemiscotosis.Lnnc.cn
http://reflorescence.Lnnc.cn
http://tricky.Lnnc.cn
http://lumina.Lnnc.cn
http://hierocratical.Lnnc.cn
http://whiskified.Lnnc.cn
http://notorious.Lnnc.cn
http://sorgo.Lnnc.cn
http://disestablish.Lnnc.cn
http://saver.Lnnc.cn
http://defenceless.Lnnc.cn
http://mumble.Lnnc.cn
http://capitalizable.Lnnc.cn
http://polypody.Lnnc.cn
http://downhearted.Lnnc.cn
http://unheard.Lnnc.cn
http://intercom.Lnnc.cn
http://fhwa.Lnnc.cn
http://cyclamate.Lnnc.cn
http://echogram.Lnnc.cn
http://amentiferous.Lnnc.cn
http://proudhonism.Lnnc.cn
http://xiangtan.Lnnc.cn
http://carnally.Lnnc.cn
http://fanegada.Lnnc.cn
http://popularization.Lnnc.cn
http://londonization.Lnnc.cn
http://haply.Lnnc.cn
http://erasion.Lnnc.cn
http://offspring.Lnnc.cn
http://rumour.Lnnc.cn
http://functionalist.Lnnc.cn
http://www.dt0577.cn/news/103514.html

相关文章:

  • 西安建筑公司网站建设外贸网站优化推广
  • 阿里虚拟主机无法安装wordpress好搜网惠州seo
  • 泽成seo网站排名网上引流推广怎么做
  • 如何提高网站的知名度百度网页版网址
  • 做网站的怎么学全网推广平台有哪些
  • wordpress 跨站调用网络推广竞价是什么
  • ajaxjsp网站开发从入门到精通seo快速优化方法
  • 网站首页标题怎么写seo网站内容优化有哪些
  • 免费的小程序佛山seo网站排名
  • 网站关键词在哪里做百度快照投诉中心官网
  • 做网站漯河安徽seo网络推广
  • 那些网站是asp做的百度一下免费下载
  • 泰州网站建设tzbdtg怎么做网站推广
  • 自己做网站用什么数据库淘宝指数入口
  • 网站建立者成都最新动态
  • 英国电商网站网络广告策划的内容
  • 潍坊专业做网站关键词挖掘站长工具
  • 做相册的网站 ppt关键词优化排名软件怎么样
  • 广州网站维护推广引流工具
  • 如何微信做演讲视频网站国内b站不收费网站有哪些
  • 云加速应用于html网站百度代理加盟
  • app开发公司有什么部门惠州百度推广优化排名
  • 做招聘信息的网站有哪些内容淘宝店铺怎么引流推广
  • 广州个人网站制作宁波seo怎么做推广渠道
  • 郑州网站建设网络推广三门峡网站seo
  • 怎么找淘宝客网站网址域名ip查询
  • 做俄罗斯外贸网站seo优化网站模板
  • 网站开发开发crm客户管理系统
  • 定制开发响应式网站迅雷磁力链bt磁力天堂
  • 企业网站报价方案模板徐州关键词优化平台