zoukankan      html  css  js  c++  java
  • C++-传值与传引用的差别

    //值传递与引用传递的差别
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void fiddle(int in1, int &in2)
    {
    	in1 = in1 + 100;
    	in2 = in2 + 100;
    	cout << "The values are ";
    	cout << setw(5) << in1;
    	cout << setw(5) << in2 << endl;
    }
    
    int main()
    {
    	int v1 = 7, v2 = 12;
    	cout << "The values are:";
    	cout << setw(5) << v1;
    	cout << setw(5) << v2 << endl;
    	fiddle(v1, v2);
    	cout << "The values are:";
    	cout << setw(5) << v1;
    	cout << setw(5) << v2 << endl;
    	system("pause");
    	return 0;
    }


    执行结果:

    7 12

    107 112

    7 112 


    能够得出结论,假设穿引用将会改变变量最初的值,而假设传值在函数中使用后并不会改变其原来的值

  • 相关阅读:
    Catalan数
    C# & LINQ 对象克隆
    Rotate Image
    反转链表
    QtCreator调试程序时GDB崩溃
    Regular Expression Matching
    Wildcard Matching
    DFA与NFA
    Set Matrix Zeroes
    PCA原理
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5105462.html
Copyright © 2011-2022 走看看