当前位置: 首页 > news >正文 伊春住房和城乡建设局网站新农村建设 网站 news 2025/10/27 13:02:14 伊春住房和城乡建设局网站,新农村建设 网站,专业网页设计,上海企业网站推广方法C中只能有一个实例的单例类 前面讨论的 President 类很不错#xff0c;但存在一个缺陷#xff1a;无法禁止通过实例化多个对象来创建多名总统#xff1a; President One, Two, Three; 由于复制构造函数是私有的#xff0c;其中每个对象都是不可复制的#xff0c;但您的目…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 operatorcout The name of the President is: ;cout President::GetInstance().GetName() endl;return 0; }输出 The name of the President is: Abraham Lincoln分析 第 2843 行的 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( )是在第 1419 行实现的 它使用静态变量 onlyInstance 确保有且只有一个 President 实例。 为更好地理解这一点可以认为第 17 行只执行一次静态初始化因此 GetInstance( )返回唯一一个President 实例而不管您如何频繁地调用 President:: GetInstance( )。 该文章会更新欢迎大家批评指正。 推荐一个零声学院的C服务器开发课程个人觉得老师讲得不错 分享给大家LinuxNginxZeroMQMySQLRedis fastdfsMongoDBZK流媒体CDNP2PK8SDocker TCP/IP协程DPDK等技术内容 点击立即学习C/C后台高级服务器课程 文章转载自: http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.lzttq.cn.gov.cn.lzttq.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn 查看全文 http://www.tj-hxxt.cn/news/253890.html 相关文章: 晋江论坛网站平台建设网站公司 基金会网站开发方案wordpress 弹出视频 天津市城乡和住房建设厅网站企业怎么注册163邮箱 东莞南城网站开发公司做外贸网站一般多少钱 网站的建设与规划方案沈阳网站制作哪家好 奢侈品商城网站建设方案网站被挂马原因 个人外贸网站开发电商网站多少钱 手机怎么上wap网站安庆做网站哪个公司好 松花江避暑城建设网站制作网站主要包括哪些步骤 成都网站logo设计网站站点不安全 住房和城乡建设部网站 挂证通报寿县网站建设 绍兴兴住房和城乡建设局网站坪山网站建设价位 南京网站开发询南京乐识山东省建设工程协会网站 建三江建设局网站成都网站建设科 建设工程材料登记备案申请网站WordPress上放广告 网站横幅怎么做搭建域名服务器 岳阳网站开发网站运营阳江房产网 登陆江西建设厅三类人员的网站iapp论坛源码 没有icp备案的网站网站建设属于哪个类目 湖北网站开发公司昆山网页设计报价 电商网站开发模版济南建设工程有限公司 如何设计网站布局做电影网站需要注意什么软件 阿凡达营销网站杭州租房网站建设 织梦网站开发东莞网站优化推广 乐清建网站哪家强天津市建设工程质量协会网站 国外上市公司网站建设phpcms网站模板 注册公司上海台州seo外链推广代理 苏州网站建设搜王道下拉天津企业网站设计制作 信息网站开发wordpress怎么登录网站后台 装修公司的网站怎么做深圳网站制作 公司