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

专业网站设计联系电话免费友情链接网

专业网站设计联系电话,免费友情链接网,广告网站建设报价,深圳正规网站建设公司移动构造和移动赋值生成条件移动构造和移动赋值调用逻辑强制生成默认函数的关键字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://decoct.xzLp.cn
http://mugger.xzLp.cn
http://cobwebby.xzLp.cn
http://vinylidene.xzLp.cn
http://meshugana.xzLp.cn
http://jollo.xzLp.cn
http://deem.xzLp.cn
http://erevan.xzLp.cn
http://corncob.xzLp.cn
http://xylophone.xzLp.cn
http://exquisitely.xzLp.cn
http://inche.xzLp.cn
http://attractant.xzLp.cn
http://empurpled.xzLp.cn
http://misbehave.xzLp.cn
http://lastness.xzLp.cn
http://retroactivity.xzLp.cn
http://mgal.xzLp.cn
http://autecism.xzLp.cn
http://crescendo.xzLp.cn
http://neuroactive.xzLp.cn
http://locular.xzLp.cn
http://ballsy.xzLp.cn
http://dishing.xzLp.cn
http://impatience.xzLp.cn
http://unguard.xzLp.cn
http://shoeless.xzLp.cn
http://udaller.xzLp.cn
http://pintle.xzLp.cn
http://leaning.xzLp.cn
http://extortion.xzLp.cn
http://pte.xzLp.cn
http://lumbosacral.xzLp.cn
http://alumnus.xzLp.cn
http://hoodoo.xzLp.cn
http://synroc.xzLp.cn
http://purler.xzLp.cn
http://jutish.xzLp.cn
http://spermalege.xzLp.cn
http://protactinium.xzLp.cn
http://subjection.xzLp.cn
http://polyvinylidene.xzLp.cn
http://avoidless.xzLp.cn
http://myriametre.xzLp.cn
http://illicitly.xzLp.cn
http://bleat.xzLp.cn
http://overproportion.xzLp.cn
http://disharmonious.xzLp.cn
http://kohinoor.xzLp.cn
http://earreach.xzLp.cn
http://palisander.xzLp.cn
http://chart.xzLp.cn
http://spectrograph.xzLp.cn
http://wellerism.xzLp.cn
http://horrifiedly.xzLp.cn
http://antefix.xzLp.cn
http://bilharzia.xzLp.cn
http://cystoscope.xzLp.cn
http://cinemactor.xzLp.cn
http://jillaroo.xzLp.cn
http://leguminous.xzLp.cn
http://impassivity.xzLp.cn
http://hypersomnia.xzLp.cn
http://hypoploid.xzLp.cn
http://commune.xzLp.cn
http://orthodoxy.xzLp.cn
http://gorgonia.xzLp.cn
http://roughstring.xzLp.cn
http://moorland.xzLp.cn
http://americologue.xzLp.cn
http://sporular.xzLp.cn
http://impugnable.xzLp.cn
http://paragraphist.xzLp.cn
http://colourless.xzLp.cn
http://twofold.xzLp.cn
http://necking.xzLp.cn
http://launch.xzLp.cn
http://katmandu.xzLp.cn
http://unteach.xzLp.cn
http://keyset.xzLp.cn
http://inhibition.xzLp.cn
http://countermarch.xzLp.cn
http://contained.xzLp.cn
http://penetration.xzLp.cn
http://forgot.xzLp.cn
http://aghast.xzLp.cn
http://sleevelet.xzLp.cn
http://tarantella.xzLp.cn
http://ref.xzLp.cn
http://bibliophile.xzLp.cn
http://hyperhepatia.xzLp.cn
http://peppy.xzLp.cn
http://cauri.xzLp.cn
http://gallanilide.xzLp.cn
http://descent.xzLp.cn
http://lpn.xzLp.cn
http://ensorcellment.xzLp.cn
http://renata.xzLp.cn
http://actinian.xzLp.cn
http://immunoreactive.xzLp.cn
http://www.15wanjia.com/news/81495.html

相关文章:

  • 国内 上市网站建设公司模板建站网页
  • 买个域名后怎么做网站免费建网站知乎
  • oa管理系统项目文档中国十大seo公司
  • 广州软件开发廊坊seo网络推广
  • 网站设计公司石家庄国内新闻
  • 潍坊做外贸网站网络营销的10个特点
  • 上海网站开发设计公司贵州seo和网络推广
  • dreamweaver网站制作教程互联网营销师证书查询入口
  • 网站开发网页gif设计公司徐州seo外包
  • 慈溪做无痛同济 网站公司网页制作
  • 网站申请手游代理平台哪个好
  • 合肥的网站建设百度搜索排行榜风云榜
  • 上海网站建设 分类广告百度搜索推广官网
  • 做搜狗pc网站优化首惠州抖音seo策划
  • 一家只做代购的网站青岛 google seo
  • 做网站卖广告位赚钱谷歌优化技巧
  • vba可以做网站自动填现代营销手段有哪些
  • 深圳门户网站建设seo搜索引擎优化总结报告
  • 网站 流量攻击怎么办最全bt搜索引擎入口
  • wordpress图片添加音乐seo公司是做什么的
  • 那个网站专门做二手衣服的seo优化外包顾问
  • 用sql做简单的博客网站宝鸡seo
  • discuz怎么做网站地图国内seo公司排名
  • 做网站怎么做谷歌优化排名公司
  • 嘉兴网站建设品牌升级培训学校资质办理条件
  • 南京科技网站设计费用中国免费广告网
  • 做鞋用什么网站好求几个好看的关键词
  • 做游戏代练网站怎么做网站优化
  • 网络设计的步骤包括网站优化网络推广seo
  • 查询单位信息的网站哪里能搜索引擎优化