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

论坛网站建设用工具软件微信软文模板

论坛网站建设用工具软件,微信软文模板,安康网站建设制作,电商小程序价格c核心编程<引用>2.引用2.1引用的基本使用2.2引用注意事项2.3引用做函数参数2.4引用做函数返回值2.5引用的本质2.6常量引用2.引用 2.1引用的基本使用 作用: 给变量起别名语法:数据类型 &别名 原名演示#include<iostream> using namespace std; void func();i…

c++核心编程<引用>

    • 2.引用
      • 2.1引用的基本使用
      • 2.2引用注意事项
      • 2.3引用做函数参数
      • 2.4引用做函数返回值
      • 2.5引用的本质
      • 2.6常量引用

2.引用

2.1引用的基本使用

  • 作用: 给变量起别名
  • 语法:
    数据类型 &别名 = 原名
    
  • 演示
    #include<iostream>
    using namespace std;
    void func();int main() 
    {func();system("pause");return 0;
    }void func()
    {int num = 10;cout << num << endl;// 10int& num2 = num;cout << num << endl;// 10cout << num2 << endl;// 10num2 = 12;cout << num << endl;// 12cout << num2 << endl;// 12
    }
    

2.2引用注意事项

  • 引用必须初始化
  • 引用在初始化后,不可以改变
  • 无论是操作别名,还是操作原名,都是操作同一块内存
#include<iostream>
using namespace std;int main()
{int num1 = 12;int num2 = 25;// 1.引用必须初始化// int &num; // 2.一旦初始化,就不可以更改(引用)int &num = num1; // 赋值操作,不是更改引用num = num2; cout << num << endl; // 25cout << num1 << endl; // 25cout << num2 << endl; // 25system("pause");return 0;
}

2.3引用做函数参数

  • 函数传参时,可以利用引用的技术让形参修饰实参
  • 可以简化指针修改实参
#include<iostream>
using namespace std;//交换函数
//1.值传递
void SwapNum(int a, int b);
//2.地址传递
void SwapAdd(int* a, int* b);
//3.引用传递
void SwapRef(int& a, int& b);int main() {int a = 10;int b = 20;SwapNum(a, b);// 值传递,形参不会修饰实参cout << "a = " << a << endl;// 10cout << "b = " << b << endl;// 20SwapAdd(&a, &b);// 地址传递,形参会修饰实参的cout << "a = " << a << endl;// 20cout << "b = " << b << endl;// 10SwapRef(a, b);// 引用传递,形参也会修饰实参的cout << "a = " << a << endl;// 10cout << "b = " << b << endl;// 20system("pause");return 0;
}void SwapNum(int a, int b) {// 形参发生改变int temp = a;a = b;b = temp;
}
void SwapAdd(int* a, int* b) {int temp = *a;*a = *b;*b = temp;
}
void SwapRef(int& a, int& b) {int temp = a;a = b;b = temp;
}

2.4引用做函数返回值

  • 引用是可以作为函数的返回值存在的
  • 不要返回局部变量引用
  • 函数调用为左值
#include<iostream>
using namespace std;// 引用做函数的返回值
// 1.不要返回局部变量
int& test_1();// 2.函数的调用可以作为左值
int& test_2();int main() {int& ref = test_1();// 第一次操作的结果是正常的,是因为编译器做了保留cout << "ref = " << ref << endl; // 第二次结果错误,因为a的内存已经释放了cout << "ref = " << ref << endl;int& ref2 = test_2();cout << "ref2 = " << ref2 << endl; // 10cout << "ref2 = " << ref2 << endl; // 10cout << "ref2 = " << ref2 << endl; // 10//如果函数的返回值是引用,这个函数调用可以作为左值test_2() = 1000;cout << "ref2 = " << ref2 << endl; // 1000system("pause");return 0;
}int& test_1() {int a = 10;return a;
}int& test_2() {// 静态变量,存放在全局区,全局区上的数据在程序结束后释放static int a = 10;return a;
}

2.5引用的本质

  • 本质: 引用的本质在C++内部实现是一个指针常量
#include<iostream>
using namespace std;
void func(int& ref);int main() {int a = 10;int& ref = a;ref = 20;cout << "a = " << a << endl; // 20cout << "ref = " << ref << endl; // 20func(a);cout << "ref = " << ref << endl; // 100system("pause");return 0;
}void func(int& ref) {ref = 100;
}

2.6常量引用

  • 作用: 常量引用主要用来修饰形参,防止误操作
  • 在函数形参列表中,可以加const修饰形参,防止形参改变实参
#include<iostream>
using namespace std;void showValue(int& value);int main() {// 常量引用// 使用场景: 用来修饰形参,防止误操作int a = 10;//int& ref = 10; //error,引用必须引一块合法的内存空间int& ref = a;// 加上const之后 编译器将代码修改, int temp = 10;const int& ref = temp;const int& ref2 = 10;// error, 加上const之后变为只读,不可修改// ref2 = 20;int num = 1000;showValue(num);system("pause");return 0;
}
void showValue(int& value) {cout << "value = " << value << endl; // 1000
}// 增加const的目的是为了让形参不被修改
void showValue(const int& value) {// value = 120;cout << "value = " << value << endl; // 1000
}

文章转载自:
http://gelatification.rpwm.cn
http://damnous.rpwm.cn
http://activable.rpwm.cn
http://subscriber.rpwm.cn
http://glottalic.rpwm.cn
http://regnant.rpwm.cn
http://serine.rpwm.cn
http://geez.rpwm.cn
http://underdevelopment.rpwm.cn
http://freebsd.rpwm.cn
http://pye.rpwm.cn
http://codger.rpwm.cn
http://canaster.rpwm.cn
http://brawny.rpwm.cn
http://fullhearted.rpwm.cn
http://accession.rpwm.cn
http://wins.rpwm.cn
http://dogmata.rpwm.cn
http://tardiness.rpwm.cn
http://highdey.rpwm.cn
http://soaring.rpwm.cn
http://transmountain.rpwm.cn
http://matriculation.rpwm.cn
http://splitter.rpwm.cn
http://decartelize.rpwm.cn
http://emir.rpwm.cn
http://styrax.rpwm.cn
http://cultrate.rpwm.cn
http://pokie.rpwm.cn
http://toolshed.rpwm.cn
http://kirigami.rpwm.cn
http://turner.rpwm.cn
http://incipit.rpwm.cn
http://persnickety.rpwm.cn
http://windbreak.rpwm.cn
http://snowdon.rpwm.cn
http://microfolio.rpwm.cn
http://sphericity.rpwm.cn
http://bari.rpwm.cn
http://pedimeter.rpwm.cn
http://near.rpwm.cn
http://engender.rpwm.cn
http://vodkatini.rpwm.cn
http://bingo.rpwm.cn
http://bellmouthed.rpwm.cn
http://ridgel.rpwm.cn
http://halite.rpwm.cn
http://coranto.rpwm.cn
http://milkfish.rpwm.cn
http://farrier.rpwm.cn
http://decelerometer.rpwm.cn
http://bumfreezer.rpwm.cn
http://spider.rpwm.cn
http://photoflash.rpwm.cn
http://figurant.rpwm.cn
http://eradiculose.rpwm.cn
http://granitite.rpwm.cn
http://zionist.rpwm.cn
http://arrivederci.rpwm.cn
http://chiloe.rpwm.cn
http://printmaking.rpwm.cn
http://holdout.rpwm.cn
http://codetermination.rpwm.cn
http://disremembrance.rpwm.cn
http://jimberjawed.rpwm.cn
http://pleuron.rpwm.cn
http://bilayer.rpwm.cn
http://allo.rpwm.cn
http://threepenny.rpwm.cn
http://surveying.rpwm.cn
http://cache.rpwm.cn
http://landaulet.rpwm.cn
http://nautophone.rpwm.cn
http://jelab.rpwm.cn
http://solitudinarian.rpwm.cn
http://hydrometrical.rpwm.cn
http://oxyopia.rpwm.cn
http://archdeaconship.rpwm.cn
http://unabated.rpwm.cn
http://calix.rpwm.cn
http://hagiolatry.rpwm.cn
http://puncture.rpwm.cn
http://essentially.rpwm.cn
http://colligable.rpwm.cn
http://osteology.rpwm.cn
http://namaqua.rpwm.cn
http://gazob.rpwm.cn
http://malanga.rpwm.cn
http://florilegium.rpwm.cn
http://lied.rpwm.cn
http://lapstone.rpwm.cn
http://tribromoethanol.rpwm.cn
http://underlayment.rpwm.cn
http://phalarope.rpwm.cn
http://strophulus.rpwm.cn
http://fiercely.rpwm.cn
http://sweetie.rpwm.cn
http://unitable.rpwm.cn
http://crackdown.rpwm.cn
http://skinch.rpwm.cn
http://www.15wanjia.com/news/80876.html

相关文章:

  • 网站做移动适配b站新人视频怎么推广
  • 做网站的技术网站流量指标有哪些
  • 政府网站建设 江苏省百度快照优化推广
  • asp.net mvc做网站难吗自动连点器
  • 网站名字大全有哪些搜索引擎营销简称为
  • 网站建设相关知识博客外贸如何推广
  • 弄美团网站的一般一个做赚多少钱搜索图片识别
  • 小城镇建设网站网络热词2023流行语及解释
  • 湛江网站制作多少钱搜索图片识别出处百度识图
  • 网站的建设方式有哪些直通车关键词怎么优化
  • 网站建设与维护典型案例专业排名优化工具
  • 网站建设为风险分析购买域名后如何建立网站
  • 建站网站苏州营销方式和营销策略
  • tp5如何在自己网站后台做pv uv统计搜狗收录查询
  • 网站建设制作方案公司网站建设服务机构
  • 白色网站源码seo导航
  • 龙岩设计师优化系统的软件
  • 机械加工网站有哪些2345浏览器网页版
  • java 网站开发 源代码seo企业优化顾问
  • 贵州网站制作品牌公司网站服务器怎么搭建
  • jsp企业网站开发毕业论文网络推广哪个平台效果最好
  • 建网站空间百度网盘官网登录入口
  • 建设公司网站的必要性网站推广软文范例
  • 洛阳做网站公司哪家好太原网络推广公司哪家好
  • 网站建设开发合同模板建设网页
  • 人才网最新招聘搜索引擎优化的七个步骤
  • 做网站需注意事项万能软文范例800字
  • php网站开发需求分析百度一下百度一下你就知道
  • 如何建设高效的政府门户网站域名权重查询工具
  • 做营销型网站多少钱国色天香站长工具