当前位置: 首页 > news >正文 自己的服务器如何做网站界面设计的软件 news 2025/10/22 11:58:26 自己的服务器如何做网站,界面设计的软件,制作营销型网站公司,学传媒以后能干什么13.1 拷贝、赋值与析构函数 拷贝控制是C++中类设计的重要组成部分,用于管理对象的复制、赋值和销毁过程。理解并正确实现拷贝控制函数(拷贝构造函数、拷贝赋值运算符和析构函数)对于编写健壮和高效的C++程序至关重要。 13.1.1 拷贝构造函数 拷贝构造函数用于创建对象的副…13.1 拷贝、赋值与析构函数 拷贝控制是C++中类设计的重要组成部分,用于管理对象的复制、赋值和销毁过程。理解并正确实现拷贝控制函数(拷贝构造函数、拷贝赋值运算符和析构函数)对于编写健壮和高效的C++程序至关重要。 13.1.1 拷贝构造函数 拷贝构造函数用于创建对象的副本。它的声明形式如下: ClassName(const ClassName other); 当对象被复制时,拷贝构造函数会被调用。例如,当对象以值传递的方式作为参数传递或从函数返回时,拷贝构造函数将被调用。 示例代码 #include iostreamclass Example { public:Example(int v) : value(v) {}Example(const Example other) : value(other.value) {std::cout "Copy constructor called" std::endl;}int getValue() const { return value; } private:int value; };void printExample(Example e) {std::cout "Value: " e.getValue() std::endl; }int main() {Example ex1(10);Example ex2 = ex1; // 调用拷贝构造函数printExample(ex1); // 调用拷贝构造函数return 0; } 13.1.2 拷贝赋值运算符 拷贝赋值运算符用于将一个对象的值赋给另一个对象。它的声明形式如下: ClassName operator=(const ClassName other); 拷贝赋值运算符通常用于对象已经存在的情况下进行赋值操作。 示例代码 #include iostreamclass Example { public:Example(int v) : value(v) {}Example(const Example other) : value(other.value) {std::cout "Copy constructor called" std::endl;}Example operator=(const Example other) {if (this != other) {value = other.value;std::cout "Copy assignment operator called" std::endl;}return *this;}int getValue() const { return value; } private:int value; };int main() {Example ex1(10);Example ex2(20);ex2 = ex1; // 调用拷贝赋值运算符std::cout "ex2 value: " ex2.getValue() std::endl;return 0; } 13.1.3 析构函数 析构函数用于释放对象使用的资源。它的声明形式如下: ~ClassName(); 析构函数在对象的生命周期结束时被自动调用,例如当对象超出作用域或被显式删除时。 示例代码 #include iostreamclass Example { public:Example(int v) : value(v) {}~Example() {std::cout "Destructor called" std::endl;}int getValue() const { return value; } private:int value; };int main() {Example ex1(10);std::cout "ex1 value: " ex1.getValue() std::endl;return 0; } 13.1.4 三/五法则 C++中有一个重要的设计原则,称为“三/五法则”,意思是如果一个类定义了析构函数、拷贝构造函数或拷贝赋值运算符中的一个,那么它很可能需要定义所有这三个函数。随着C++11标准引入移动语义,这个原则扩展为“五法则”,包括移动构造函数和移动赋值运算符。 示例代码 #include iostream #include utilityclass Example { public:Example(int v) : value(new int(v)) {}Example(const Example other) : value(new int(*other.value)) {std::cout "Copy constructor called" std::endl;}Example operator=(const Example other) {if (this != other) {delete value;value = new int(*other.value);std::cout "Copy assignment operator called" std::endl;}return *this;}~Example() {delete value;std::cout "Destructor called" std::endl;}// C++11移动构造函数Example(Example other) noexcept : value(other.value) {other.value = nullptr;std::cout "Move constructor called" std::endl;}// C++11移动赋值运算符Example operator=(Example other) noexcept {if (this != other) {delete value;value = other.value;other.value = nullptr;std::cout "Move assignment operator called" std::endl;}return *this;}int getValue() const { return *value; } private:int* value; };int main() {Example ex1(10);Example ex2 = std::move(ex1); // 调用移动构造函数Example ex3(20);ex3 = std::move(ex2); // 调用移动赋值运算符return 0; } 13.1.5 阻止拷贝 在某些情况下,我们可能希望阻止对象被拷贝或赋值。可以通过将拷贝构造函数和拷贝赋值运算符声明为delete来实现这一点。 示例代码 class NonCopyable { public:NonCopyable() = default;NonCopyable(const NonCopyable ) = delete;NonCopyable operator=(const NonCopyable ) = delete; };int main() {NonCopyable obj1;// NonCopyable obj2 = obj1; // 错误:拷贝构造函数被删除// NonCopyable obj3;// obj3 = obj1; // 错误:拷贝赋值运算符被删除return 0; } 重点与难点分析 重点: 拷贝构造函数:了解何时会调用拷贝构造函数,掌握其实现方法。拷贝赋值运算符:理解拷贝赋值运算符的使用场景和实现细节。析构函数:熟悉析构函数的作用和调用时机。三/五法则:掌握何时需要同时实现拷贝构造函数、拷贝赋值运算符和析构函数,理解移动语义下的五法则。阻止拷贝:了解如何通过delete关键字阻止对象的拷贝和赋值。难点: 自我赋值检测:在拷贝赋值运算符中处理自我赋值的情况。资源管理:确保在析构函数中正确释放资源,避免内存泄漏和其他资源管理问题。编写健壮的拷贝控制函数:理解并正确实现拷贝控制函数,以确保对象在复制、赋值和销毁过程中行为一致且安全。实现移动语义:正确实现移动构造函数和移动赋值运算符,提高程序性能。练习题解析 练习13.1:编写一个类,包含拷贝构造函数、拷贝赋值运算符和析构函数,测试它们的调用情况。示例代码:#include iostreamclass Example { public:Example(int v) : value(v) {}Example(const Example other) : value(other.value) {std::cout "Copy constructor called" std::endl;}Example operator=(const Example other) {if (this != other) {value = other.value;std::cout "Copy assignment operator called" std::endl;}return *this;}~Example() {std::cout "Destructor called" std::endl;}int getValue() const { return value; } private:int value; };int main() {Example ex1(10);Example ex2 = ex1; // 调用拷贝构造函数ex2 = ex1; // 调用拷贝赋值运算符return 0; } 练习13.2:修改练习13.1的类,使其包含一个指向动态内存的指针,确保拷贝控制函数正确管理该内存。示例代码#include iostreamclass Example { public: Example(int v) : value(new int(v)) {} Example(const Example other) : value(new int(*other.value)) {std::cout "Copy constructor called" std::endl; } Example operator=(const Example other) {if (this != other) {delete value;value = new int(*other.value);std::cout "Copy assignment operator called" std::endl;}return *this; } ~Example() {delete value;std::cout "Destructor called" std::endl; } int getValue() const { return *value; } private: int* value; };int main() {Example ex1(10);Example ex2 = ex1; // 调用拷贝构造函数ex2 = ex1; // 调用拷贝赋值运算符std::cout "ex2 value: " ex2.getValue() std::endl;return 0; }练习13.3:编写一个类,包含移动构造函数和移动赋值运算符,测试它们的调用情况。示例代码:#include iostreamclass Example { public:Example(int v) : value(new int(v)) {}Example(const Example other) : value(new int(*other.value)) {std::cout "Copy constructor called" std::endl;}Example operator=(const Example other) {if (this != other) {delete value;value = new int(*other.value);std::cout "Copy assignment operator called" std::endl;}return *this;}Example(Example other) noexcept : value(other.value) {other.value = nullptr;std::cout "Move constructor called" std::endl;}Example operator=(Example other) noexcept {if (this != other) {delete value;value = other.value;other.value = nullptr;std::cout "Move assignment operator called" std::endl;}return *this;}~Example() {delete value;std::cout "Destructor called" std::endl;}int getValue() const { return *value; } private:int* value; };int main() {Example ex1(10);Example ex2 = std::move(ex1); // 调用移动构造函数Example ex3(20);ex3 = std::move(ex2); // 调用移动赋值运算符return 0; } 练习13.4:编写一个类,阻止其对象的拷贝和赋值。示例代码:#include iostreamclass NonCopyable { public:NonCopyable() = default;NonCopyable(const NonCopyable ) = delete;NonCopyable operator=(const NonCopyable ) = delete; };int main() {NonCopyable obj1;// NonCopyable obj2 = obj1; // 错误:拷贝构造函数被删除// NonCopyable obj3;// obj3 = obj1; // 错误:拷贝赋值运算符被删除return 0; } 总结与提高 本节总结: 掌握了拷贝控制的基本概念和操作,包括拷贝构造函数、拷贝赋值运算符和析构函数。理解了拷贝控制函数在对象复制、赋值和销毁过程中的作用和调用时机。学会了在类中正确实现拷贝控制函数,以确保对象在不同生命周期阶段的正确行为。理解了“三/五法则”的重要性,学会了在类设计中同时实现必要的拷贝控制函数和移动语义。掌握了如何通过delete关键字阻止对象的拷贝和赋值。提高建议: 多练习拷贝控制函数的实现:通过编写更多涉及拷贝控制的类,熟悉各种管理方法的用法,提高对拷贝控制的理解和实现能力。深入理解资源管理的原理:通过阅读文档和相关书籍,深入理解资源管理的实现原理和使用场景,提高编写高效代码的能力。优先使用智能指针:在实际项目中,尽量使用智能指针管理动态内存,以减少手动内存管理带来的错误,提高代码的可读性和可维护性。优化移动语义:在类设计中,合理运用移动构造函数和移动赋值运算符,提高程序的性能和资源利用效率。13.2 拷贝控制与资源管理 拷贝控制函数在管理资源时非常重要,特别是当类涉及动态内存分配或其他资源(如文件句柄、网络连接等)时。通过正确实现拷贝控制函数,可以确保资源在对象复制、赋值和销毁过程中得到正确的管理和释放。 13.2.1 拷贝控制与动态内存 在涉及动态内存的类中,拷贝构造函数、拷贝赋值运算符和析构函数的实现需要特别注意,以确保内存安全。 示例代码 #include iostream #include algorithm // for std::copyclass DynamicArray { public:DynamicArray(size_t size) : size(size), data(new int[size]()) {}DynamicArray(const DynamicArray other) : size(other.size), data(new int[other.size]) {std::copy(other.data, other.data + other.size, data);std::cout "Copy constructor called" std::endl;}DynamicArray operator=(const DynamicArray other) {if (this != other) { 文章转载自: http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.lhztj.cn.gov.cn.lhztj.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.wfhnz.cn.gov.cn.wfhnz.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.rkdnm.cn.gov.cn.rkdnm.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.ycnqk.cn.gov.cn.ycnqk.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn 查看全文 http://www.tj-hxxt.cn/news/239606.html 相关文章: 做图片网站会被今天微博热搜前十名 惠州建设局网站首页广告公司名字400个 百度站长 添加网站周口市城乡建设局网站 做音乐网站之前的准备做打鱼网站的代理 三亚网站开发滁州seo 洛阳网站建设启辰网络电脑网站 源码 电商网站建设效果上海专业做网站公司 林州网站制作网络设计规划师 那里有网站建设成都市住房和城乡建设局 张家港哪家做企业网站中文com域名注册 花都区水务建设管理中心官方网站用友财务软件官方网站 简约 时尚 高端 网站建设优化过程中十大技巧 秦皇岛昌黎县建设局网站青岛建站模板厂家 佛山企业网站设计制作上海金融网站建设公司 东营网站设计公司3000ok新开传奇网站 天津建设网投标网站外贸公司业务流程 网站建设 程序开发嘉鱼网站建设优化 网站设置在哪里找到网易企业邮箱登录入口网页版 scratch在线编程网站东莞建筑公司招聘信息 做网站 推广国内商务网络公司排名 兴义做网站的可信赖的做pc端网站 网站源码采集服务哪家好网站制作 湖北省和城乡建设厅官方网站百度竞价网站怎么做 ps做网站原形外贸app网站开发 做服装网站服务织梦 网站地图 wifi管理网站企业网站建设公司电话 网站建设hairongsoft网站建设公司设计网页的工具 容桂网站建设找顺的云南网站设计平台 做电商卖玉器的网站福建宁德建设局网站 站长统计app官方网站内蒙古住房与建设官方网站