2019/c++

c++ reference variables

fw93 2018. 3. 18. 11:51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
int func(int & num) {
    num = num+3;
    return num+3;
}
 
int main() {
    int num = 50;
    int & num2 = num;
    num2 = func(num);
    num2++;
    cout << num2 << endl// 57
    cout << num << endl// 57
    while (1) {}
    return 0;
}
cs


& 변수는 같은주소 변수를 뜻한다. 주소참조 포인터 변수와는 비슷하지만 다른 용도이다