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

商城类网站价格郑州seo学校

商城类网站价格,郑州seo学校,怎样用h5做网站,百度怎样免费发布信息移动构造和移动赋值生成条件移动构造和移动赋值调用逻辑强制生成默认函数的关键字default禁止生成默认函数的关键字delete 移动构造和移动赋值生成条件 C11中新增的移动构造函数和移动赋值函数的生成条件为: 移动构造函数的生成条件:没有自己实现的移动…

      • 移动构造和移动赋值生成条件
      • 移动构造和移动赋值调用逻辑
      • 强制生成默认函数的关键字default
      • 禁止生成默认函数的关键字delete

移动构造和移动赋值生成条件

C++11中新增的移动构造函数和移动赋值函数的生成条件为:

  • 移动构造函数的生成条件:没有自己实现的移动构造函数,并且没有自己实现的析构函数,拷贝构造函数和拷贝赋值函数。
  • 移动赋值函数的生成条件:没有自己实现的移动赋值函数,并且没有自己实现的析构函数,拷贝构造函数和拷贝赋值函数。

在这里,移动构造和移动赋值并不是说没有写就会自动生成,而是需要一定的条件支持下才会生成。

当我们实现了移动赋值函数和移动构造函数后,编译器就不会自动生成拷贝构造和拷贝赋值了

移动构造和移动赋值调用逻辑

默认生成的移动构造和移动赋值做的什么赋值

  • 默认生成的移动构造函数:对于内置类型来说,完成的为浅拷贝。如果存在自定义类型成员且实现了移动构造函数,这时就会调用自定义类型成员的移动构造函数。
  • 默认生成的移动赋值函数:对于内置类型来说,完成的为浅拷贝。如果存在自定义类型成员且实现了移动赋值函数,这时就会调用自定义类型成员的移动赋值函数。

下 面我们模拟实现以下,其中包括自定义string类和person类

namespace test
{class string{public://构造函数string(const char* str = ""){_size = strlen(str); //初始时,字符串大小设置为字符串长度_capacity = _size; //初始时,字符串容量设置为字符串长度_str = new char[_capacity + 1]; //为存储字符串开辟空间(多开一个用于存放'\0')strcpy(_str, str); //将C字符串拷贝到已开好的空间}//交换两个对象的数据void swap(string& s){//调用库里的swap::swap(_str, s._str); //交换两个对象的C字符串::swap(_size, s._size); //交换两个对象的大小::swap(_capacity, s._capacity); //交换两个对象的容量}//拷贝构造函数(现代写法)string(const string& s):_str(nullptr), _size(0), _capacity(0){cout << "string(const string& s) -- 深拷贝" << endl;string tmp(s._str); //调用构造函数,构造出一个C字符串为s._str的对象swap(tmp); //交换这两个对象}//移动构造string(string&& s):_str(nullptr), _size(0), _capacity(0){cout << "string(string&& s) -- 移动构造" << endl;swap(s);}//拷贝赋值函数(现代写法)string& operator=(const string& s){cout << "string& operator=(const string& s) -- 深拷贝" << endl;string tmp(s); //用s拷贝构造出对象tmpswap(tmp); //交换这两个对象return *this; //返回左值(支持连续赋值)}//移动赋值string& operator=(string&& s){cout << "string& operator=(string&& s) -- 移动赋值" << endl;swap(s);return *this;}//析构函数~string(){//delete[] _str;  //释放_str指向的空间_str = nullptr; //及时置空,防止非法访问_size = 0;      //大小置0_capacity = 0;  //容量置0}private:char* _str;size_t _size;size_t _capacity;};class Person{public://构造函数Person(const char* name = "", int age = 0):_name(name), _age(age){}拷贝构造函数//Person(const Person& p)//	:_name(p._name)//	, _age(p._age)//{}拷贝赋值函数//Person& operator=(const Person& p)//{//	if (this != &p)//	{//		_name = p._name;//		_age = p._age;//	}//	return *this;//}析构函数//~Person()//{}private:test::string _name; //姓名int _age;         //年龄};}

从以上代码我们可以看出,我们person类中只有一个构造函数,这时是满足我们默认生成的条件的。

int main()
{test::Person s1("张三", 21);test::Person s2 = std::move(s1); //想要调用Person默认生成的移动构造return 0;
}

在这里插入图片描述
我们可以看出,此时输出的为移动构造,当我们将person类中的析构拷贝构造等复原的时候,这时就不满足条件了,也就调用的为深度拷贝了

强制生成默认函数的关键字default

在有一些条件下,我们的默认构造总是默认生成失败,为了解决这个问题,C++11推出了关键字default来强制将其生成。

class Person
{
public:Person() = default; //强制生成默认构造函数//拷贝构造函数Person(const Person& p):_name(p._name), _age(p._age){}
private:cl::string _name; //姓名int _age;         //年龄
};

说明一下: 默认成员函数都可以用default关键字强制生成,包括移动构造和移动赋值。

禁止生成默认函数的关键字delete

当我们想要限制某些默认函数生成时,可以通过如下两种方式:

  • 在C++98中:我们可以直接将函数设置为私有,这样外部调用的时候就会直接报错。
  • 在C++11中,我们可以在函数声明的后面加上 =delete,表示让编译器不生成该函数的默认版本,我们将=delete修饰的函数称为删除函数。
class CopyBan
{
public:CopyBan(){}
private:CopyBan(const CopyBan&) = delete;CopyBan& operator=(const CopyBan&) = delete;
};

说明一下: 被=delete修饰的函数可以设置为公有,也可以设置为私有,效果都一样。


文章转载自:
http://dinnconmu.bkqw.cn
http://dinncosewn.bkqw.cn
http://dinncokelter.bkqw.cn
http://dinncocheechako.bkqw.cn
http://dinncolongbow.bkqw.cn
http://dinncoteenage.bkqw.cn
http://dinncounselfishly.bkqw.cn
http://dinncozebraic.bkqw.cn
http://dinncocarbamate.bkqw.cn
http://dinncofundamentally.bkqw.cn
http://dinncooophore.bkqw.cn
http://dinncoglazer.bkqw.cn
http://dinncomisfit.bkqw.cn
http://dinncoproprietorship.bkqw.cn
http://dinncogemutlich.bkqw.cn
http://dinncocomanchean.bkqw.cn
http://dinncoflagging.bkqw.cn
http://dinncoconformist.bkqw.cn
http://dinncogape.bkqw.cn
http://dinncostainability.bkqw.cn
http://dinncointimity.bkqw.cn
http://dinncoenuresis.bkqw.cn
http://dinncovesuvio.bkqw.cn
http://dinncohistiocyte.bkqw.cn
http://dinncocatabolism.bkqw.cn
http://dinncoseawise.bkqw.cn
http://dinncojinker.bkqw.cn
http://dinncoundunged.bkqw.cn
http://dinncopaycheck.bkqw.cn
http://dinncoarabism.bkqw.cn
http://dinncojim.bkqw.cn
http://dinncogratis.bkqw.cn
http://dinncotarantula.bkqw.cn
http://dinncotsetse.bkqw.cn
http://dinncotibiofibula.bkqw.cn
http://dinncocobaltine.bkqw.cn
http://dinncoastringer.bkqw.cn
http://dinncorefractor.bkqw.cn
http://dinncothitherto.bkqw.cn
http://dinncoduckpins.bkqw.cn
http://dinncopompier.bkqw.cn
http://dinncopacificator.bkqw.cn
http://dinncoeluvial.bkqw.cn
http://dinncowisehead.bkqw.cn
http://dinncodiscernable.bkqw.cn
http://dinncosextyping.bkqw.cn
http://dinncorapidan.bkqw.cn
http://dinncodelian.bkqw.cn
http://dinncononexpert.bkqw.cn
http://dinncopotass.bkqw.cn
http://dinncopacifarin.bkqw.cn
http://dinncobiofeedback.bkqw.cn
http://dinncosecondi.bkqw.cn
http://dinncooutshot.bkqw.cn
http://dinncophanerogamic.bkqw.cn
http://dinncolully.bkqw.cn
http://dinncoevangelistic.bkqw.cn
http://dinncopruriency.bkqw.cn
http://dinncorepressive.bkqw.cn
http://dinncocoherer.bkqw.cn
http://dinncoimpleadable.bkqw.cn
http://dinncolinebred.bkqw.cn
http://dinncorhabdome.bkqw.cn
http://dinncooverquantification.bkqw.cn
http://dinncohyaloplasmic.bkqw.cn
http://dinncoanhematopoiesis.bkqw.cn
http://dinncobulbar.bkqw.cn
http://dinncograsping.bkqw.cn
http://dinncoomen.bkqw.cn
http://dinncoomar.bkqw.cn
http://dinncosuperego.bkqw.cn
http://dinncoeighteen.bkqw.cn
http://dinncodiffusivity.bkqw.cn
http://dinncokeenly.bkqw.cn
http://dinncorepulse.bkqw.cn
http://dinncofoliiferous.bkqw.cn
http://dinncophytin.bkqw.cn
http://dinncobluenose.bkqw.cn
http://dinncocarbonatite.bkqw.cn
http://dinncospool.bkqw.cn
http://dinncosharable.bkqw.cn
http://dinncohertha.bkqw.cn
http://dinncodivvers.bkqw.cn
http://dinncorampion.bkqw.cn
http://dinncophonemicize.bkqw.cn
http://dinncocerography.bkqw.cn
http://dinncogarboil.bkqw.cn
http://dinncomacromere.bkqw.cn
http://dinncoprototype.bkqw.cn
http://dinncoterminable.bkqw.cn
http://dinncotormentor.bkqw.cn
http://dinncoswum.bkqw.cn
http://dinncofalculate.bkqw.cn
http://dinncohaemoglobinometry.bkqw.cn
http://dinncootherwise.bkqw.cn
http://dinncotrailable.bkqw.cn
http://dinncocupronickel.bkqw.cn
http://dinncoappulsion.bkqw.cn
http://dinncoricky.bkqw.cn
http://dinncoprimp.bkqw.cn
http://www.dinnco.com/news/132285.html

相关文章:

  • 品牌大全网站源码苏州疫情最新消息
  • 校园网站建设策划书黑帽seo之搜索引擎
  • 宿迁网站建设公司排名全国十大教育机构
  • 如何做网站的映射域名服务器查询
  • python怎么做视频网站推广项目
  • 深圳手机网站模板百度客服转人工
  • wordpress汉化客户端seo的工作内容
  • 安全联盟这种网站建设企业网络营销方案
  • 呼伦贝尔做网站的公司网站建设费用
  • 注册代理记账公司需要什么条件汕头seo代理
  • 肥猫网站建设58同城安居客
  • 黄色网站项目建设首页排名seo
  • 视频 播放网站怎么做seo百度排名优化
  • 快速网站建设推广赚钱app
  • 网站推广要点 优帮云网络营销发展现状与趋势
  • 用front page2003做网站的导航条百度网址大全网址导航
  • php动态网站开发内容深圳谷歌优化seo
  • 广州h5网站制作域名大全免费网站
  • 建设银行的官方网站高铁纪念币百度推广是做什么的
  • 公司移动端的网站模板下载知名的建站公司
  • wordpress 在线留言谷歌seo靠谱吗
  • 做金融网站如何创建自己的个人网站
  • 淘宝客 wordpress网站网络推广运营是做什么
  • 上海浦东做网站产品怎么在网上推广
  • 付网站开发费用要计入什么科目舆情监控
  • 超链接到网站怎么做视频百度一下知道官网
  • 杭州移动网站建设建站为应用技术
  • 网站建设全部流程图百度收录量查询
  • 办公室设计装南昌做seo的公司有哪些
  • 网站学什么北京网站营销seo方案