zoukankan      html  css  js  c++  java
  • 对引用和指针使用以及函数返回引用和指针类型的理解

    #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

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

  • 相关阅读:
    SqlServer2008 安装经验日志总结
    Moile手机开发日志总结若干问题
    安装和卸载Android应用程序(apk包)
    Android Intent 常见用法总结
    vs2008+sqlserver2008 同一台服务器webconfig 数据连接串的配置要点
    注意了android日期控件月份比实际少一个月
    Android 蓝牙开发研究
    [javascript] 怎样在javascript里面调试object
    [Ubuntu] 转载:ubuntu apache2配置
    [Ubuntu] 如何在Ubuntu11.04将PHP5.3降级到PHP5.2
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/1994516.html
Copyright © 2011-2022 走看看