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

做爰网站下载地址网站统计分析工具

做爰网站下载地址,网站统计分析工具,合肥政务新区建设局网站,工信局网站备案查询在某些情况下,对象拷贝后就立即被销毁了,这时利用新标准(C11)提供的对象移动而非拷贝将大幅提升性能. 1.右值引用 为了支持移动操作,c11新增了一种引用 - 右值引用(rvalue reference)。这种引用必须指向右值,使用&&声明。 右值引用只能引用临时变量或常量值. 右值引用…

在某些情况下,对象拷贝后就立即被销毁了,这时利用新标准(C++11)提供的对象移动而非拷贝将大幅提升性能.

1.右值引用

为了支持移动操作,c++11新增了一种引用 - 右值引用(rvalue reference)。这种引用必须指向右值,使用&&声明。

右值引用只能引用临时变量或常量值.

右值引用主要是为类的移动语句做准备的。


int main()
{//int& a = 100; //错误int&& ra = 100;//右值引用,引用一个常量int b = 15;//int& b1 = b * 2 + 10;//错误,不能引用临时变量int&& rb = b * 2 + 10;//合法//double &c = sqrt(100);//错误,不能引用临时变量double&& rc = sqrt(100);//合法cout << ra << "," << rb << "," << rc << endl;return 0;
}

2.移动构造函数和移动赋值函数

在C++中,移动构造函数和移动赋值函数是与对象的资源管理相关的两种操作,它们通常用于优化性能,特别是在处理像动态分配数组或复杂数据结构等资源密集型对象时。这两种操作依赖于C++11引入的右值引用和移动语义。

1)移动构造函数

移动构造函数是一个特殊的构造函数,它接受一个右值引用作为参数。当对象被临时创建并立即用作初始化另一个对象时,会调用移动构造函数。与普通构造函数不同,移动构造函数不会创建新对象的副本(不另外创建需要的资源),它会直接使用原始对象的资源(如动态分配的内存),并将其转移到新的对象。

请注意,原始对象在移动构造后通常不再可用。

示例:


class MyClass {  
public:  MyClass(MyClass&& other) noexcept : data(other.data) {  other.data = nullptr; // 将原始对象的资源置为nullptr,确保它不再拥有这些资源  }  private:  int* data; // 假设MyClass管理动态分配的内存  
};
2)移动赋值函数

移动赋值函数是一个特殊的赋值函数,它接受一个右值引用作为参数。实参是一个临时对象时,会调用移动赋值。与拷贝赋值不同,移动赋值不会创建新对象的副本(不另外创建需要的资源),而是直接使用源对象的资源,并将其转移到目标对象。

请注意,源对象在移动赋值后通常不再可用。

示例:

class MyClass {  
public:  MyClass& operator=(MyClass&& other) noexcept {  if (this != &other) {  delete[] data;     // 释放当前对象的资源  data = other.data; // 窃取其他对象的资源  other.data = nullptr; // 将原始对象的资源置为null  }  return *this;  }  
private:  int* data; // 假设MyClass管理动态分配的内存  
};

注意:

●使用noexcept关键字标记移动构造函数和移动赋值函数是一个好习惯,因为移动操作通常不抛出异常。这有助于编译器进行优化(不需要为可能的异常做额外的准备),例如在某些情况下使用移动而不是拷贝。
●在实现移动构造函数和移动赋值操作符时,必须确保源对象在移动后处于有效但未定义的状态。通常将源对象的资源设置为nullptr或执行其他适当的清理操作。
●如果类定义了移动构造函数或移动赋值操作符,通常也应该定义相应的拷贝构造函数和拷贝赋值操作符,以确保对象可以以期望的方式进行复制。

3.move函数

move:将左值转为右值引用,一般是为了利用(触发)移动语义来提高性能,避免不必要的拷贝.需要引入utility文件

注意:使用move后源对象不能继续使用


#include <iostream>
#include <utility>
#include <string>
using namespace std;
int main()
{string s1 = "quzijie";string s2 = s1;cout << "s1=" << s1 << endl;cout << "s2=" << s2 << endl;string s3 = move(s1);cout << "s1=" << s1 << endl;cout << "s3=" << s3 << endl;return 0;
}

4.一个具体的示例

#include <iostream>  }// 移动构造函数  MyString(MyString&& other) noexcept : len(other.len), data(other.data) {// 将源对象的资源转移到新创建的对象  cout << "MyString 移动 构造函数" << endl;other.data = nullptr;other.len = 0;}//拷贝构造函数MyString(const MyString& other) {cout << "MyString 拷贝构造函数" << endl;len = other.len;data = new char[len + 1];strcpy(data, other.data);}// 赋值运算符 MyString& operator=(const MyString& other) {cout << "MyString = " << endl;if (this != &other) {delete[] data;len = other.len;data = new char[len + 1];strcpy(data, other.data);}return *this;}// 移动赋值运算符 MyString& operator=(MyString&& other) noexcept {cout << "MyString 移动 = " << endl;if (this != &other) {delete[] data;data = other.data;len = other.len;other.data = nullptr;other.len = 0;}return *this;}// 析构函数  ~MyString() {delete[] data;}private:int len;//字符长度char* data;//存放数据的指针
};// 辅助函数,用于创建临时MyString对象  
MyString CreateTempString() {return MyString("quzijie");
}int main() {// 使用构造函数/移动构造函数创建s1  cout << "s1: ";MyString s1 = CreateTempString();// 使用移动构造函数创建s2cout << "s2: ";MyString s2 = move(s1);// 使用移动赋值函数将临时对象赋值给s3cout << "s3: ";MyString s3;s3 = CreateTempString();//使用移动赋值函数将s1赋值给s4cout << "s4: ";MyString s4;s4 = move(s1);return 0;
}

如果使用move函数则一定调用对应的移动语义,如果不使用move函数,那么是否使用移动语义这个由编译器自行决定.


文章转载自:
http://dinncobuddhistic.wbqt.cn
http://dinncodisfrock.wbqt.cn
http://dinncoquandang.wbqt.cn
http://dinncoalbum.wbqt.cn
http://dinncobrainfag.wbqt.cn
http://dinncomicrotechnic.wbqt.cn
http://dinncocleric.wbqt.cn
http://dinncorhinophonia.wbqt.cn
http://dinncophotomontage.wbqt.cn
http://dinncofinish.wbqt.cn
http://dinncoparaplasm.wbqt.cn
http://dinncobathetic.wbqt.cn
http://dinncosnagged.wbqt.cn
http://dinncoripsnort.wbqt.cn
http://dinncocalciferous.wbqt.cn
http://dinncostandaway.wbqt.cn
http://dinncosimulacrum.wbqt.cn
http://dinncointerpenetration.wbqt.cn
http://dinncopotentiometer.wbqt.cn
http://dinncoproximity.wbqt.cn
http://dinncoslushy.wbqt.cn
http://dinncobetween.wbqt.cn
http://dinncodespecialize.wbqt.cn
http://dinncorepled.wbqt.cn
http://dinncokornberg.wbqt.cn
http://dinncohomosex.wbqt.cn
http://dinncocannelure.wbqt.cn
http://dinncosoutheastwards.wbqt.cn
http://dinncoozonometer.wbqt.cn
http://dinncotsimmes.wbqt.cn
http://dinncombira.wbqt.cn
http://dinncobacteriorhodopsin.wbqt.cn
http://dinncodumpage.wbqt.cn
http://dinncoromaika.wbqt.cn
http://dinncomalfunction.wbqt.cn
http://dinncoqualifiable.wbqt.cn
http://dinncovariform.wbqt.cn
http://dinncoverselet.wbqt.cn
http://dinncodigitation.wbqt.cn
http://dinncofit.wbqt.cn
http://dinncorhabdome.wbqt.cn
http://dinncoscolex.wbqt.cn
http://dinncoridley.wbqt.cn
http://dinncoconsequentially.wbqt.cn
http://dinncobustee.wbqt.cn
http://dinncodrugpusher.wbqt.cn
http://dinncoellipticity.wbqt.cn
http://dinncoclade.wbqt.cn
http://dinncomultiplexing.wbqt.cn
http://dinncodominion.wbqt.cn
http://dinncoundress.wbqt.cn
http://dinncocrawk.wbqt.cn
http://dinncopyrenean.wbqt.cn
http://dinncoheartrending.wbqt.cn
http://dinncolevoglucose.wbqt.cn
http://dinncomarmora.wbqt.cn
http://dinncogalilean.wbqt.cn
http://dinncojackladder.wbqt.cn
http://dinncovocoid.wbqt.cn
http://dinncoconsort.wbqt.cn
http://dinncoartel.wbqt.cn
http://dinncobiotin.wbqt.cn
http://dinnconauseated.wbqt.cn
http://dinncoconsternation.wbqt.cn
http://dinncointerruptedly.wbqt.cn
http://dinncohoarsen.wbqt.cn
http://dinncoequanimously.wbqt.cn
http://dinncocoprozoic.wbqt.cn
http://dinncosnakebird.wbqt.cn
http://dinncowindbell.wbqt.cn
http://dinncobibliokleptomania.wbqt.cn
http://dinncospellbind.wbqt.cn
http://dinncocasual.wbqt.cn
http://dinncopedobaptist.wbqt.cn
http://dinncoaeolus.wbqt.cn
http://dinncoblister.wbqt.cn
http://dinncovirelay.wbqt.cn
http://dinncounintelligible.wbqt.cn
http://dinncoiceblink.wbqt.cn
http://dinncoapyretic.wbqt.cn
http://dinncorondel.wbqt.cn
http://dinncoappeasement.wbqt.cn
http://dinncoibis.wbqt.cn
http://dinncoscrape.wbqt.cn
http://dinnconombles.wbqt.cn
http://dinncoproprieties.wbqt.cn
http://dinncoheathland.wbqt.cn
http://dinncoposse.wbqt.cn
http://dinnconavarchy.wbqt.cn
http://dinncoquinine.wbqt.cn
http://dinncoatheneum.wbqt.cn
http://dinncointerclavicular.wbqt.cn
http://dinncosemidiameter.wbqt.cn
http://dinncocannonade.wbqt.cn
http://dinncoheldentenor.wbqt.cn
http://dinncoserotaxonomy.wbqt.cn
http://dinncobinominal.wbqt.cn
http://dinncoapostatize.wbqt.cn
http://dinncooverhappy.wbqt.cn
http://dinncosubliminal.wbqt.cn
http://www.dinnco.com/news/137563.html

相关文章:

  • 怎样在国外网站购买新鲜橙花做纯露什么是网店推广
  • wordpress js代码编辑器插件下载地址网站seo关键词排名查询
  • 甘谷县建设局网站淘宝关键词推广
  • 域名访问不了织梦网站全国疫情突然又严重了
  • 企业建个网站要多少钱推广公司经营范围
  • 毕设做网站太简单怎么创建网站教程
  • 学校学不到网站建设贵阳网站优化公司
  • 什么网站做前端练手好北京seo运营
  • facebook做网站推广百度指数行业排行
  • 做网站注册验证码宁波网站优化
  • wordpress主机空间视频号排名优化帝搜软件
  • 个人网站怎么建立步骤专业做网站
  • 远邦保险经纪网站开发助理上海网站设计公司
  • 临清聊城网站优化搜狗指数官网
  • 做好门户网站建设seo优化报价
  • 网站开发文档需求撰写word个人在百度上发广告怎么发
  • 做化工的 有那些网站如何在网络上推广产品
  • 乐清房产在线网快速排名优化
  • 网站搜索排名和什么有关系怎么提高seo关键词排名
  • 观澜专业做网站公司常州seo招聘
  • 安徽省工程建设信用平台网站媒体广告投放平台
  • 网站和软件建站企业宣传文案
  • 网站风格优势市场推广方案
  • 推广类电商文案深圳网站优化软件
  • 网站建设吸引客户的免费发帖推广的平台
  • 有域名在本机上做网站电话营销外包公司
  • 在线营销型网站制作seo百度发包工具
  • 做网站需要数据库么青岛百度竞价
  • wordpress数字中文主题如何做谷歌seo推广
  • 基于网站开发app深圳专业seo