zoukankan      html  css  js  c++  java
  • 按值传递&&按引用传递&&按地址传递

    按值传递:不改变外部对象


    按引用传递&&按地址传递:允许改变外部对象

    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;

    //passing by refedrence
    void f(int &r)
    {
        cout << "r = " << r << endl;
        cout << "&r = " << &r << endl;
        r = 5 ;
        cout << "r = " << r << endl;
    }

    //passing by value
    void f(int a)
    {
        cout<<"a= "<<a<<endl;
        a = 5;
        cout << "a= " << a <<endl;
    }

    //passing by address
    void f(int *p)
    {
        cout << "p = " << p << endl;
        cout << "*p = " << *p << endl;
        *p = 5;
        cout << "p = " << p << endl;
    }

    int main()
    {
    //passing by reference
    int x = 47 ;
    cout << "x = " << x <<endl;
    cout << "&x = " << &x << endl;
    f(x);//Looks like pass-by-value
          //is actually pass by reference
    cout << "x = " << x << endl;
    //output
    x = 47
    &x = 0x7FFF1D5C9B4C
    r = 47
    &r = 0x7FFF1D5C9B4C
    r = 5
    x = 5

    //passing by address
    int x = 47;
    cout << "x = " << x << endl;
    cout << "&x = " << &x << endl;
    f(&x);
    cout << "x = " << x << endl;
    //output
    ////////////////////////////////
    x = 47
    &x = 0x7FFF9746423c
    p = 0x7FFF9746423c
    *p = 47
    p = 0x7FFF9746423c
    x = 5


    //passing by value
    int x = 97 ;
    cout << "x=" << x <<endl;
    f(x);
    cout << "x=" << x <<endl;
    //output
    ///////////////////////////////
    x = 47
    a = 47
    a = 5
    x = 47
    }

  • 相关阅读:
    $.getJSON()
    seconds
    ini_set
    validation
    component
    ini_set();
    长期阅读英文技术博客的好处
    用xml还是json
    单​手​打​字
    洛谷P1141 01迷宫
  • 原文地址:https://www.cnblogs.com/shaoguangleo/p/2805843.html
Copyright © 2011-2022 走看看