[C++] Reference와 Pointer의 차이
초기화 (Initialization)
// Must initialize the reference at the first
int& ra; // Wrong
int& ra = a; // Correct
// Pointer can do this
int *a;
Reference는 한 번 초기화 이후 다른 변수를 가리킬 수 없음
int a = 10;
int &ra = a; // ra가 a를 참조
int b = 3;
ra = b; // ra에 b의 값(3)을 대입할 뿐 (재지정 X)
b = 10; // a == ra == 3, b == 10
int *pa = &a; // pa는 최초로 a를 지정
pa = &b; // b로 재지정 가능
Reference는 메모리에 존재할 수도, 하지 않을 수도 있음
1. 이미 존재하는 변수의 다른 이름
void func()
{
int a = 10;
int &ra = a; // Not exist in memory space
}
- 컴파일러의 최적화에 의해 &ra가 실제로 메모리를 차지할 수도 있고, 차지하지 않을 수도 있다고 한다
2. 함수의 매개 변수
void func(int &a, int &b); // Arguments are stored in stack frame memory
- 스택 프레임에서 메모리를 차지한다
3. 클래스 멤버 변수
class A
{
public:
int a;
int& b = a;
}; // sizeof(A) = 4+4 = 8; // Takes memory
- 클래스 멤버 변수로서의 레퍼런스는 반드시 메모리를 차지한다
레퍼런스의 배열(Array of references)?
- 아래 식들은 모두 불가능 illegal
- &&a: references to references
- & a[]: arrays of references
- &*a: pointers to references
// 임의의 두 변수가 선언되어(연속된 메모리에 배치되어 있지 않은)
// 이 변수를 레퍼런스의 배열로 선언하는 것은 불가능
int a, b; // Random location in stack
int& arr[2] = {a , b}; // Illegal: Impossible to reference the non-adjacent variables
// 연속된 메모리에 선언된 변수들을
// 배열로서 레퍼런스로 선언하는 것은 가능
int arr[3] = {1, 2, 3}; // adjacent memory locations
int (&ref)[3] = arr; // Legal: Variables are adjacent