文章标题

VC++引用

#include <iostream>
using namespace std;
void change (int &a, int &b);
void main()
{
    int a = 5;
    int &b = a; //用&表示申明一个引用,引用必须在申明时进行初始化,引用一旦初始化,它就代表一块特定的内存,再也不能代表其他的内存。
    int c = 3;
    b = c;
    cout << "a is" << a << endl << "b is" << b << endl << "c is" << c << endl;
    int x = 5;
    int y = 3;
    cout << "original X=" << x << endl;
    cout << "original Y=" << y << endl;

    change(x, y);

    cout << "changed X=" << x << endl;
    cout << "changed Y=" << y << endl;
    cin.get();
}
void change(int &a, int &b)
{
    a = a + b;  //a、b地址总和赋值给a
    b = a - b;
    a = a - b;
}

你可能感兴趣的:(vc++)