对引用和指针使用以及函数返回引用和指针类型的理解

23 篇文章 0 订阅

#include "stdafx.h"
#include <iostream>
using namespace std;
int globalJ =999;
 
//返回值
int test1()
{
    int j =1;
    cout<<"in test1(),[return value]  the varaible j's address :"<<&j<<endl;
 
    return j;
}
 
//使用局部变量,返回引用
int& test2()
{
 int j =998;
 cout<<"in test2(),[use field variable and return reference]  the local varaible J's address :"<<&j<<endl;
 cout<<"in test2(),[use field variable and return reference]  the local varaible J's value :"<<j<<endl;
 
  return j;
}
 
//使用全局变量,返回引用
int& test3()
{
   
    cout<<"in test3(),[use global variable and return reference] the varaible globalJ's address :"<<&globalJ<<endl;
   
    return globalJ;
}
 
 
//返回指针
int* test4()
{
    int j =998;
    cout<<"in test4(),[use field variable and return pointer]  the local varaible J's address :"<<&j<<endl;
 cout<<"in test4(),[use field variable and return pointer]  the local varaible J's value :"<<j<<endl;
 
 
  return &j; 
}
//返回指针
int* test5()
{
    cout<<"in test5()[use global variable and return pointer] , the varaible globalJ's address :"<<&globalJ<<endl;
    return &globalJ; 
}
 
 
int main(int argc, char* argv[])
{
    printf("Hello functions!/n");
   
    int testresultvalue =0;
    testresultvalue = test1();
    cout<<"testResultValue address :"<<&testresultvalue <<endl;
    cout<<"testResultValue value:"<<testresultvalue<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
 
    int & testResultReference  = test2();
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
    testResultReference = test3();
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
    testResultReference = 4;
    cout<<"reset to 4"<<endl;
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
   
    cout<<"slit line----------------------------------"<<endl;
 
    int & testResultReference2 = test3();
    cout<<"testResultReference2 address :"<<&testResultReference2 <<endl;
    cout<<"testResultReference2 value:"<<testResultReference2<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
    int* testResultPtr;
    testResultPtr = test4();
    cout<<"testResult address :"<<testResultPtr <<endl;
    cout<<"testResult value:"<<*testResultPtr<<endl;
    cout<<"slit line----------------------------------"<<endl;
   
    testResultPtr = test5();
    cout<<"testResult address :"<<testResultPtr <<endl;
    cout<<"testResult value:"<<*testResultPtr<<endl;
    cout<<"slit line----------------------------------"<<endl;

    int temp;
    cin>>temp;
 
    return 0;
}

 

然后我们来分析结果,

test1 是返回值,没有什么好说的。

test2

image

本地变量的J的值是998,引用后得到的值是1245056. 因为在function作用域以外这块内存的东西就被释放掉了,所以值就不一致了。实际上,应该用全局变量(或类变量)。

注意:J的内存地址和引用变量的地址是一样的。

在编译中会报警告信息:

warning C4172: returning address of local variable or temporary

test3

image

globalJ全局变量的地址是00478DC0,但引用变量testresultreference的内存地址仍然是0012FF14。

结论:引用变量一次赋值后即为只读,即使再次赋值,其引用对象不变。

image

根据上面的情况我们新定义一个引用变量testresultreference2, globalJ的地址和引用变量地址是一致的。

test4

image

这个例子跟引用有点相似。虽然指针指向的仍是本地变量的地址,但值发生了改变。

结论:指针指向局部变量,一旦超出变量域,值就有问题了。

test5

image

结论:请注意指针变量可以多次赋值,这样就改变了指向的对象。这点跟引用对象的使用方式不同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值