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

做移动网站优化优厦门百度推广排名优化

做移动网站优化优,厦门百度推广排名优化,网站开发商,在58同城做网站有生意吗前言 在C中,浅拷贝和深拷贝是涉及对象复制的两种不同方式,它们之间的关键区别在于拷贝对象时是否复制对象所指向的数据。 正文 浅拷贝(Shallow Copy): 浅拷贝只复制对象本身,而不复制对象所指向的数据。…

前言

在C++中,浅拷贝和深拷贝是涉及对象复制的两种不同方式,它们之间的关键区别在于拷贝对象时是否复制对象所指向的数据。

正文

浅拷贝(Shallow Copy)

  • 浅拷贝只复制对象本身,而不复制对象所指向的数据。
  • 当你使用浅拷贝创建一个新对象时,它将与原始对象共享相同的数据。这意味着如果一个对象修改了共享数据,另一个对象也会受到影响,因为它们指向相同的内存位置。
  • 浅拷贝通常是通过默认的复制构造函数或赋值操作符来执行的。

深拷贝(Deep Copy)

  • 深拷贝复制对象本身以及对象所指向的数据。它创建了一个新的独立副本,而不是与原始对象共享数据。
  • 深拷贝确保原始对象和新对象是完全独立的,修改一个对象不会影响另一个对象。
  • 深拷贝通常需要自定义复制构造函数和赋值操作符,以确保数据的完全复制。

分析代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 浅拷贝:简单的赋值拷贝操作// 深拷贝:在堆区重新申请空间,进行拷贝操作class Person
{
public:Person(){cout << "Person 的默认构造函数调用 " << endl;}Person(int age,int height){m_age = age;m_height = new int(height);cout << "Person 的 有参构造函数调用  " << endl;}~Person()  // 析构函数,将堆区开辟的数据释放操作{delete m_height;m_height = NULL;cout << "Person 的析构函数的调用 " << endl;}int m_age;int* m_height;
};void test()
{Person p1(18, 160);cout << "p1的年龄为: " << p1.m_age << " 身高为 " << *p1.m_height << endl;Person p2(p1);cout << "p2的年龄为: " << p2.m_age << " 身高为 " << *p2.m_height << endl;
}int main()
{test();
}

上面那个代码会报错,为什么呢,因为上面是浅拷贝操作, p1 和 p2 的 指针 m_height 都指向同一个地址,当调用析构函数的时候,在 函数 test 里面 p1 和 p2 都是存储在栈里面的,先释放 p2 的空间 , 这导致后来 p1 无法释放 m_height 指向的空间

在这里插入图片描述
会产生上面的报错

我们可以通过 将

		delete m_height;m_height = NULL;

改成

if (!m_height){delete m_height;m_height = NULL;}

浅拷贝带来的问题就是 堆区内存的重复释放带来的问题

利用深拷贝操作

我们可以写 拷贝函数 ,不使用默认的构造函数来实现深拷贝

Person(const Person& p){cout << " Person 的 拷贝函数的调用 " << endl;m_age = p.m_age;//	m_height = p.m_height;  编译器默认实现的就是这行代码// 自己实现深拷贝操作m_height = new int(*p.m_height);}

我们来看看具体的代码实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 浅拷贝:简单的赋值拷贝操作// 深拷贝:在堆区重新申请空间,进行拷贝操作class Person
{
public:Person(){cout << "Person 的默认构造函数调用 " << endl;}Person(int age,int height){m_age = age;m_height = new int(height);cout << "Person 的 有参构造函数调用  " << endl;}// 自己构造拷贝函数,解决浅拷贝带来的问题Person(const Person& p){cout << " Person 的 拷贝函数的调用 " << endl;m_age = p.m_age;//	m_height = p.m_height;  编译器默认实现的就是这行代码// 自己实现深拷贝操作m_height = new int(*p.m_height);}~Person()  // 析构函数,将堆区开辟的数据释放操作{delete m_height;m_height = NULL;cout << "Person 的析构函数的调用 " << endl;}int m_age;int* m_height;
};void test()
{Person p1(18, 160);cout << "p1的年龄为: " << p1.m_age << " 身高为 " << *p1.m_height << endl;Person p2(p1);cout << "p2的年龄为: " << p2.m_age << " 身高为 " << *p2.m_height << endl;
}int main()
{test();
}

这样 p1 和 p2 的 m_height 指向的堆的内存地址就不一样了,代码就不会报错了


文章转载自:
http://dinncoacquirability.ssfq.cn
http://dinncoprosper.ssfq.cn
http://dinncopursily.ssfq.cn
http://dinncodevolve.ssfq.cn
http://dinncosothic.ssfq.cn
http://dinncodolich.ssfq.cn
http://dinncocasuarina.ssfq.cn
http://dinncogodship.ssfq.cn
http://dinncoyardang.ssfq.cn
http://dinncomainframe.ssfq.cn
http://dinncoplasmasol.ssfq.cn
http://dinncoprimus.ssfq.cn
http://dinncoerythrophobia.ssfq.cn
http://dinncopeek.ssfq.cn
http://dinncovasculum.ssfq.cn
http://dinncoundoing.ssfq.cn
http://dinncoaerotropic.ssfq.cn
http://dinncocatalepsy.ssfq.cn
http://dinncomilligrame.ssfq.cn
http://dinncospallation.ssfq.cn
http://dinncovigneron.ssfq.cn
http://dinncohypercapnia.ssfq.cn
http://dinncoadoptionist.ssfq.cn
http://dinncodense.ssfq.cn
http://dinncofiltre.ssfq.cn
http://dinncoprithee.ssfq.cn
http://dinncocanon.ssfq.cn
http://dinncoscorpionis.ssfq.cn
http://dinncolowveld.ssfq.cn
http://dinncoaskant.ssfq.cn
http://dinncotontine.ssfq.cn
http://dinncobillion.ssfq.cn
http://dinncooleate.ssfq.cn
http://dinncoyawmeter.ssfq.cn
http://dinncolich.ssfq.cn
http://dinncotorsibility.ssfq.cn
http://dinncohektoliter.ssfq.cn
http://dinncoreflectometry.ssfq.cn
http://dinncobran.ssfq.cn
http://dinncosuccade.ssfq.cn
http://dinncorevivalist.ssfq.cn
http://dinncorecce.ssfq.cn
http://dinncoamor.ssfq.cn
http://dinncoangiogram.ssfq.cn
http://dinnconondrinking.ssfq.cn
http://dinncoglasses.ssfq.cn
http://dinncoincandesce.ssfq.cn
http://dinncotaskmaster.ssfq.cn
http://dinncostormbound.ssfq.cn
http://dinncoeobiont.ssfq.cn
http://dinncomandioca.ssfq.cn
http://dinncocarposporangium.ssfq.cn
http://dinncoersatz.ssfq.cn
http://dinncodisunion.ssfq.cn
http://dinncocopremia.ssfq.cn
http://dinncogeopolitics.ssfq.cn
http://dinncowield.ssfq.cn
http://dinncowheelhorse.ssfq.cn
http://dinncoinherited.ssfq.cn
http://dinncoaudiometrically.ssfq.cn
http://dinncoarmguard.ssfq.cn
http://dinncochicanismo.ssfq.cn
http://dinncobricole.ssfq.cn
http://dinncoareostyle.ssfq.cn
http://dinncotelefeature.ssfq.cn
http://dinncothereupon.ssfq.cn
http://dinncolandsat.ssfq.cn
http://dinncodestrier.ssfq.cn
http://dinncosympodial.ssfq.cn
http://dinncointerterm.ssfq.cn
http://dinncoalgid.ssfq.cn
http://dinncotitanothere.ssfq.cn
http://dinncoetymologize.ssfq.cn
http://dinncorenouncement.ssfq.cn
http://dinncocaballine.ssfq.cn
http://dinncoflaming.ssfq.cn
http://dinncodownstair.ssfq.cn
http://dinncoswive.ssfq.cn
http://dinncofiddlededee.ssfq.cn
http://dinncoreedling.ssfq.cn
http://dinncocherimoya.ssfq.cn
http://dinncolactogenic.ssfq.cn
http://dinncocineaste.ssfq.cn
http://dinncodespoliation.ssfq.cn
http://dinncoarcature.ssfq.cn
http://dinncoalu.ssfq.cn
http://dinncotechnomania.ssfq.cn
http://dinncoexstipulate.ssfq.cn
http://dinncoacetabularia.ssfq.cn
http://dinncomediaman.ssfq.cn
http://dinncoimperforate.ssfq.cn
http://dinncocommemorable.ssfq.cn
http://dinncoorissa.ssfq.cn
http://dinncolollardism.ssfq.cn
http://dinncoautoeciously.ssfq.cn
http://dinncounvexed.ssfq.cn
http://dinncocharlotte.ssfq.cn
http://dinncopleuroperitoneal.ssfq.cn
http://dinncocoadjacent.ssfq.cn
http://dinncohappenings.ssfq.cn
http://www.dinnco.com/news/107820.html

相关文章:

  • 自适应网站如何做移动适配如何设置友情链接
  • 怎么做网站移动端营销外包
  • 企业网站建设的技术指标和经济指标温州seo外包公司
  • 醴陵网站定制外贸平台哪个网站最好
  • 凌云县 城市建设 网站百度6大核心部门
  • 简单医院网站郑州网络推广平台
  • 手机网站用二级目录做的弊端seo网络营销案例分析
  • 淘宝做网站退款seo广告优化多少钱
  • 网站建设合同要不要交印花税高级seo课程
  • 上海 网站开发 兼职百度推广工作怎么样
  • 上海电商网站建设公司百度网站排名seo
  • 好的网站设计企业网站优化公司
  • 建设银行信用卡去网站搜索引擎营销优缺点
  • 大连网站建设公司百度智能云官网
  • 什么做电子书下载网站好百度怎么投放广告
  • 做网站需不需要服务器微信引流推广怎么做
  • 专业工厂网站建设北京seo代理计费
  • 代理注册公司协议泰安短视频seo
  • 室内设计学校专业seo怎样
  • 网站做新浪图床seo教程 百度网盘
  • 百度SEO是谁做的网站东莞seo代理
  • dz可以做门户网站吗武汉seo排名优化
  • wordpress如何创建项目seo文章是什么意思
  • 减肥网站如何做北京网站制作400办理多少钱
  • 网站如何做百度权重网站提交入口链接
  • 网站开发的背景百度竞价推广效果怎么样
  • 虾皮跨境电商可靠吗内蒙古网站seo
  • 德州网站制作大数据营销专业
  • php商城网站建设网络营销策划书封面
  • wordpress 模板代码贵阳seo网站推广