首页 > 开发 > C++ > 正文

C++引用的基本使用方法

2016-08-24 11:14:46  来源:慕课网
#include <iostream>
using namespace std;

/**
 * 交换变量
 */
void swap(int& x, int& y)
{
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
}

int main(void)
{
    int x = 3;
    // 传递引用
    int& y = x;

    cout << x << endl;
    cout << y << endl;
    y = 10;
    cout << x << endl;
    cout << y << endl;

    // 交换x,z
    int z = 4;
    swap(x, z);
    cout << x << endl;
    cout << z << endl;

    return 0;
}