



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
引用的符号是&,指针的符号是*先来看看一段测试代码, 1: #include stdafx.h 2: #include 3: using namespace std; 4: int globalJ =999; 5: 6: /返回值 7: int test1() 8: 9: int j =1; 10: coutin test1(),return value the varaible js address :&jendl; 11: 12: return j; 13: 14: 15: /使用局部变量,返回引用 16: int& test2() 17: 18: int j =998; 19: coutin test2(),use field variable and return reference the local varaible Js address :&jendl; 20: coutin test2(),use field variable and return reference the local varaible Js value :jendl; 21: 22: return j; 23: 24: 25: /使用全局变量,返回引用 26: int& test3() 27: 28: 29: coutin test3(),use global variable and return reference the varaible globalJs address :&globalJendl; 30: 31: return globalJ; 32: 33: 34: 35: /返回指针 36: int* test4() 37: 38: int j =998; 39: coutin test4(),use field variable and return pointer the local varaible Js address :&jendl; 40: coutin test4(),use field variable and return pointer the local varaible Js value :jendl; 41: 42: 43: return &j; 44: 45: /返回指针 46: int* test5() 47: 48: coutin test5()use global variable and return pointer , the varaible globalJs address :&globalJendl; 49: return &globalJ; 50: 51: 52: 53: int main(int argc, char* argv) 54: 55: printf(Hello functions!n); 56: 57: int testresultvalue =0; 58: testresultvalue = test1(); 59: couttestResultValue address :&testresultvalue endl; 60: couttestResultValue value:testresultvalueendl; 61: coutslit line-endl; 62: 63: 64: int & testResultReference = test2(); 65: couttestResultReference address :&testResultReference endl; 66: couttestResultReference value:testResultReferenceendl; 67: coutslit line-endl; 68: 69: testResultReference = test3(); 70: couttestResultReference address :&testResultReference endl; 71: couttestResultReference value:testResultReferenceendl; 72: testResultReference = 4; 73: coutreset to 4endl; 74: couttestResultReference address :&testResultReference endl; 75: couttestResultReference value:testResultReferenceendl; 76: 77: coutslit line-endl; 78: 79: int & testResultReference2 = test3(); 80: couttestResultReference2 address :&testResultReference2 endl; 81: couttestResultReference2 value:testResultReference2endl; 82: coutslit line-endl; 83: 84: int* testResultPtr; 85: testResultPtr = test4(); 86: couttestResult address :testResultPtr endl; 87: couttestResult value:*testResultPtrendl; 88: coutslit line-endl; 89: 90: testResultPtr = test5(); 91: couttestResult address :testResultPtr endl; 92: couttestResult value:*testResultPtrendl; 93: coutslit line-temp; 99: 100: return 0; 101: 102: 然后我们来分析结果,test1 是返回值,没有什么好说的。test2本地变量的J的值是998,引用后得到的值是1245056. 因为在function作用域以外这块内存的东西就被释放掉了,所以值就不一致了。实际上,应该用全局变量(或类变量)。注意:J的内存地址和引用变量的地址是一样的。在编译中会报警告信息:warning C4172: returning address of local variable or temporarytest3globalJ全局变量的地址是00478DC0,但引用变量testresultreference的内存地址仍然是0012FF14。结论:引用变量一次赋值后即为只读,即使再次赋值,其引用对象不变。根据上面的情况我们新定义一个引用变量testresultre
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024新版2025秋青岛版科学五四制三年级上册教学课件:第四单元 第18课 热气球上升的秘密
- 手机批量采购协议
- 银行中层面试题库及答案
- 银行政府类项目测试题及答案
- 银行行政考试题库及答案
- 银行消防试题及答案
- 河南旅游专业试题及答案
- 医学转专业试题及答案
- 销售专业试题及答案英语
- 印刷专业试题及答案
- 人教版九年级数学下册第二十六章反比例函数-作业设计
- 边坡削坡施工方案
- 浙江省通用安装工程预算定额第八册
- 乡村振兴战略实施与美丽乡村建设课件
- 中频电疗法理疗(共60张PPT)精选
- 医学信息检索与利用智慧树知到答案章节测试2023年杭州医学院
- 黑底搭配大气企业宣传商业计划书商务通用PPT模板
- GB/T 17608-2006煤炭产品品种和等级划分
- 沪教五年级数学上册第一单元测试卷
- 地下停车库设计统一规定
- 建筑装饰设计收费管理规定
评论
0/150
提交评论