博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++的引用
阅读量:2089 次
发布时间:2019-04-29

本文共 1811 字,大约阅读时间需要 6 分钟。

参考代码:

#include 
using namespace std;int testIntRef(){ int i=1; int &ri = i ; int j = i ; cout << hex << "Addr of i: " << &i << endl; cout << hex << "Addr of ri:" << &ri << endl ; cout << hex << "Addr of j: " << &j << endl ; return 0;}int testObjectRef(){ class A{ public: int ma1,ma2 ; A(int a1 , int a2){ ma1 = a1 ; ma2 = a2 ; } }; A a(1,2) ; A& ra = a ; A b =a ; cout << hex << "Addr of a: " << &a << endl; cout << hex << "Addr of ra:" << &ra << endl ; cout << hex << "Addr of b: " << &b << endl ; return 0 ;}class H{public: int mh1 , mh2 ; H(int h1 , int h2) { mh1 = h1 ; mh2 = h2 ; }};void callByRefAndValue( H& rh , H h ){ cout << hex << "Addr of rh:" << &rh << endl ; cout << hex << "Addr of h: " << &h << endl ;}int testCallByRefAndValue(){ H h(1,2) ; cout << hex << "Addr of h outer: " << &h << endl ; callByRefAndValue( h , h ) ; return 0;}H& returnRefOfEnterRef(H& hh ){ cout<<"in returnRefOfEnterRef :"<
<< &hh << endl ; return hh ;}H& returnRefOfEnterVal(H hh ){ cout<<"in returnRefOfEnterVal :"<
<< &hh << endl ; return hh ;}H returnValOfEnterRef(H& hh ){ cout<<"in returnValOfEnterRef :"<
<< &hh << endl ; return hh ;}int testReturnRef(){ H h(1,2) ; cout<<"h inializted:"<
<< &h << endl ; H& h1 = returnRefOfEnterRef( h ) ; cout<<"Get from returnRefOfEnterRef :"<
<< &h1 << endl ; H& h2 = returnRefOfEnterVal( h ) ; cout<<"Get from returnRefOfEnterVal :"<
<< &h2 << endl ; H h3 = returnValOfEnterRef( h ) ; cout<<"Get from returnValOfEnterRef :"<
<< &h3 << endl ;}int main(){ //testIntRef() ; //testObjectRef() ; //testCallByRefAndValue(); testReturnRef();}

 

转载地址:http://zemqf.baihongyu.com/

你可能感兴趣的文章
Flink示例——Window、EventTime、WaterMark
查看>>
Flink示例——State、Checkpoint、Savepoint
查看>>
Flink示例——Table、SQL
查看>>
HBase之Rowkey设计
查看>>
推荐算法——ALS模型算法分析、LFM算法
查看>>
Spark源码剖析——RpcEndpoint、RpcEnv
查看>>
Spark源码剖析——Master、Worker启动流程
查看>>
TensorFlow2 学习——MLP图像分类
查看>>
TensorFlow2 学习——CNN图像分类
查看>>
Spark源码剖析——SparkSubmit提交流程
查看>>
TensorFlow2 学习——RNN生成古诗词
查看>>
Spark源码剖析——SparkContext实例化
查看>>
基于阿里云的数据仓库架构设计
查看>>
Docker——安装、常用命令、生成镜像(Dockerfile)
查看>>
Docker——部署(tomcat、nginx、负载均衡、常见问题)
查看>>
Spark代码可读性与性能优化——示例十一(SQL与代码-蚂蚁森林示例)
查看>>
OpenCV学习——图像基础与几何变换
查看>>
OpenCV学习——图像特效
查看>>
opencv + face_recognition —— 人脸识别案例
查看>>
Spark源码剖析——Action操作、runJob流程
查看>>