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

泰州模板自助建站磁力吧最佳搜索引擎

泰州模板自助建站,磁力吧最佳搜索引擎,旅游网站开发文献综述,域名换了网站需要备案么1.函数返回 在讲解右值之前,要知道下面这个函数要进行几次拷贝以及为什么? int get_x() {int x 20;return x; }int aget_x(); 答案:两次 # 第一次 int tmpa; # 第二次 int xtmp;2.左值与右值 🍏2.1 能取地址操作的就是左值 …

1.函数返回

在讲解右值之前,要知道下面这个函数要进行几次拷贝以及为什么?

int get_x()
{int x = 20;return x;
}int a=get_x();

答案:两次

# 第一次
int tmp=a;
# 第二次
int x=tmp;

2.左值与右值

🍏2.1 能取地址操作的就是左值

int gx = 10;
int get_gx()
{return gx;
}int get_x()
{int x = 20;return x;
}get_x();  //右值
get_gx(); //右值,因为返回值是一个tmp变量

延伸一下,对 x++ 取地址和 ++x 取地址哪个可以成功?

int* p=&x++; // 错误
// 后++等价于
/* 
int func(int &a) {int b=a;a=a+1;return b; 
}
*/int *q=&++x; // 正确
// 前++等价于
/*
int func(int &a) {a=a+1;return a; 
}
*/

🍎 2.2 左值和右值的爱恨情仇

  • 左值引用不接受右值,所以只能用右值引用来接收, Type&&
int &y=-10;  // 错误
int &&y=-10; // 正确int &a=get_x();  // 错误
int &&a=get_x(); // 正确
  • 如何将左值转换为右值
    • 移动语义:std::move 可以将左值转换为右值 
    • static_cast<type &&>(xxx)

3. 赋值操作只有拷贝这一种解法吗

  • 拷贝操作:我想与一名10年水平的码农一样强,那我应该学习10年
  • 引用操作:与它共享一份大脑
  • 移动操作
    • 这个10年的码农今年150岁了去世了,把它的大脑移植过来
    • 这位码农150岁,干净长生不老了,弄死后移植过来

🍐 例子

拷贝操作:

#include <iostream>
#include <string.h>
#include <string>// copy from 【现代 C++ 语言核心特性解析】
class BigMemoryPool
{
public:static const int PoolSize = 4096;BigMemoryPool() : pool_(new char[PoolSize]) {}~BigMemoryPool(){if (pool_ != nullptr){delete[] pool_;}}BigMemoryPool(const BigMemoryPool &other) : pool_(new char[PoolSize]){std::cout << "copy" << std::endl;memcpy(pool_, other.pool_, PoolSize);}private:char *pool_;
};BigMemoryPool getPool()
{BigMemoryPool memoryPool;return memoryPool;
}int main()
{BigMemoryPool bbb = getPool();
}

输出

copy
copy

 将上述代码加入移动构造函数(这个10年的码农今年150岁了去世了,把它的大脑移植过来)

class BigMemoryPool
{
public:static const int PoolSize = 4096;BigMemoryPool() : pool_(new char[PoolSize]) {}~BigMemoryPool(){if (pool_ != nullptr){delete[] pool_;}}BigMemoryPool(BigMemoryPool &&other){std::cout << "move" << std::endl;pool_ = other.pool_;other.pool_ = nullptr;}BigMemoryPool(const BigMemoryPool &other) : pool_(new char[PoolSize]){std::cout << "copy" << std::endl;memcpy(pool_, other.pool_, PoolSize);}private:char *pool_;
};int main()
{BigMemoryPool bbb = getPool(); // 这个10年的码农今年150岁了去世了,把它的大脑移植过来
}

输出

move
move

这位码农150岁,干净长生不老了,弄死后移植过来

BigMemoryPool aaa;
BigMemoryPool ccc = std::move(aaa);

输出

move

4. Notes:

  •  纯右值也可以std::move
  • 类中未实现移动构造,std::move之后仍是拷贝
  • 右值引用仍是左值 
    int  x=10;
    int &&z=std::move(x);
    &z; // 左值
  • 右值绑定在左值上连移动构造都不会发生
    BigMemoryPool aaa;
    BigMemoryPool &&ccc = std::move(aaa);

http://www.hkea.cn/news/799492/

相关文章:

  • 网站做付费推广都需要问什么网络热词2022
  • 给男票做网站表白的软件产品市场推广计划书
  • 西安网站制作定制怎么制作自己的个人网站
  • wordpress 如何移动端盐城seo优化
  • asp.net 制作网站开发百度竞价排名软件
  • 百度爱采购推广平台天津网络推广seo
  • 福州市闽侯县建设局网站推广引流吸引人的文案
  • wordpress目录 读写权限泰安短视频seo
  • 东莞建设网站流程澎湃新闻
  • 萧县住房和城乡建设局网站seo排名推广工具
  • 企业网站php模板下载百度百科官网首页
  • 做愛視頻网站在线网页制作网站
  • 织梦pc怎么做手机网站搜索引擎优化的基础是什么
  • 课程建设网站设计源码爱站网反链查询
  • 安徽省建设业协会网站个人网页制作教程
  • 好的摄影网站推荐福州seo顾问
  • html做的好看的网站如何宣传推广产品
  • 微信手机网站制作怎么引流客源最好的方法
  • 宿州建设网站公司前端seo搜索引擎优化
  • 做王境泽表情的网站百度seo关键词优化排名
  • 怎么选择无锡网站建设虚拟主机搭建网站
  • 做原油期货关注什么网站搜索引擎优化是做什么
  • 微信小程序怎么制作游戏安卓优化清理大师
  • 胶南做网站初学者做电商怎么入手
  • 网站为什么要维护佛山网络营销推广
  • 国企网站建设报告怎么建造自己的网站
  • 免费做司考真题的网站余姚网站如何进行优化
  • 如何网站开发1688网站
  • 丽水专业网站建设价格青岛网站优化
  • 网站开发专业培训学校百度推广登录官网入口