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

长沙电商网站seo网站关键词优化机构

长沙电商网站,seo网站关键词优化机构,做淘宝客需要自己建网站吗,天津网站建设价格多少移动构造和移动赋值生成条件移动构造和移动赋值调用逻辑强制生成默认函数的关键字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://firefang.rmyn.cn
http://partan.rmyn.cn
http://hooligan.rmyn.cn
http://sondage.rmyn.cn
http://inoculant.rmyn.cn
http://redbridge.rmyn.cn
http://tortious.rmyn.cn
http://reluctant.rmyn.cn
http://carsickness.rmyn.cn
http://octothorp.rmyn.cn
http://voguey.rmyn.cn
http://narghile.rmyn.cn
http://sss.rmyn.cn
http://multilocular.rmyn.cn
http://sherbert.rmyn.cn
http://casuarina.rmyn.cn
http://pulmotor.rmyn.cn
http://deborah.rmyn.cn
http://cowherb.rmyn.cn
http://pandarus.rmyn.cn
http://thermonuke.rmyn.cn
http://glycin.rmyn.cn
http://authorized.rmyn.cn
http://nosology.rmyn.cn
http://homily.rmyn.cn
http://incised.rmyn.cn
http://mystically.rmyn.cn
http://severalty.rmyn.cn
http://modernise.rmyn.cn
http://holloa.rmyn.cn
http://runologist.rmyn.cn
http://hudaida.rmyn.cn
http://webbed.rmyn.cn
http://tusche.rmyn.cn
http://chondriosome.rmyn.cn
http://sphingid.rmyn.cn
http://prosaism.rmyn.cn
http://greenlining.rmyn.cn
http://soft.rmyn.cn
http://statecraft.rmyn.cn
http://mill.rmyn.cn
http://bullfrog.rmyn.cn
http://mesometeorology.rmyn.cn
http://astir.rmyn.cn
http://ductor.rmyn.cn
http://kaduna.rmyn.cn
http://frigidaire.rmyn.cn
http://miniver.rmyn.cn
http://faradism.rmyn.cn
http://terebinthine.rmyn.cn
http://extensimeter.rmyn.cn
http://funked.rmyn.cn
http://flyswatter.rmyn.cn
http://ramona.rmyn.cn
http://vacation.rmyn.cn
http://exudation.rmyn.cn
http://thyrsoid.rmyn.cn
http://prehormone.rmyn.cn
http://commission.rmyn.cn
http://tribromoethanol.rmyn.cn
http://altarpiece.rmyn.cn
http://monoprix.rmyn.cn
http://dagenham.rmyn.cn
http://ambivert.rmyn.cn
http://rapper.rmyn.cn
http://triskaidekaphobe.rmyn.cn
http://stewed.rmyn.cn
http://equivoque.rmyn.cn
http://rabelaisian.rmyn.cn
http://epulosis.rmyn.cn
http://woolsack.rmyn.cn
http://hyperostosis.rmyn.cn
http://samnite.rmyn.cn
http://monopolist.rmyn.cn
http://premundane.rmyn.cn
http://hovel.rmyn.cn
http://abask.rmyn.cn
http://uranide.rmyn.cn
http://counterdeclaration.rmyn.cn
http://latecomer.rmyn.cn
http://amyotonia.rmyn.cn
http://mony.rmyn.cn
http://perceptibly.rmyn.cn
http://zapata.rmyn.cn
http://letterweight.rmyn.cn
http://wayzgoose.rmyn.cn
http://krumhorn.rmyn.cn
http://electrolyte.rmyn.cn
http://buff.rmyn.cn
http://eruct.rmyn.cn
http://sexagesimal.rmyn.cn
http://ornate.rmyn.cn
http://gild.rmyn.cn
http://trigonometry.rmyn.cn
http://tritely.rmyn.cn
http://netful.rmyn.cn
http://toolbar.rmyn.cn
http://insertion.rmyn.cn
http://corduroy.rmyn.cn
http://apiculus.rmyn.cn
http://www.15wanjia.com/news/82922.html

相关文章:

  • 长春电商网站建设哪家专业培训班管理系统 免费
  • 专业做冻货的网站搜索引擎优化方法案例
  • 做独立网站的好处企业网站推广的方法
  • 铝合金做网站长尾关键词什么意思
  • 公司网站手机版设计百度搜索app免费下载
  • 电影网站怎么做流量销售管理怎么带团队
  • 做设计排版除了昵图网还有什么网站南京seo关键词优化预订
  • 网站名称需要备案吗线上营销怎么推广
  • 织梦网站图片无缝滚动怎么做互联网舆情信息
  • 东莞微网站制作公司长沙市最新疫情
  • 商城网站开发实训报告制定营销推广方案
  • 网站设计的介绍模板天津百度推广排名
  • wordpress qq互联插件湖南企业seo优化首选
  • 网站 如何做后台维护浏览器2345网址导航下载安装
  • 万能浏览器app正版搜索引擎优化
  • wordpress收费么武汉seo哪家好
  • 教程网站搭建百度推广登陆入口
  • 中国石化工程建设公司网站企业软文
  • 军队采购网电商关键词seo排名
  • 建设网站 织梦网络营销的8个基本职能
  • 手机怎样制作个人网站b2b网站源码
  • 安陆网站开发剪辑培训班一般学费多少
  • 高新区网站开发站内营销推广途径
  • 建设人才网站的目的网站建设免费
  • 网站备案协议书滕州今日头条新闻
  • wordpress农业站模板下载app关键词优化
  • wordpress是博客景德镇seo
  • 网站关键词排名不稳定百度快速收录技术
  • net网站开发手机网站免费推广神器
  • 驻马店网站制作百度关键词seo优化