참조변수는 이미 선언되어 있는 변수에 별명을 붙이는 것이다.
▶ 참조 변수 : 일반 변수의 참조형
data - type 일반 변수명; data - type & 참조 변수명 = 일반 변수명; |
포인터 | 참조 변수 |
int a = 10; int *b = &a; *b += 20; |
int a = 10; int &b = a; b += 20; |
▶ 참조 포인터 변수 : 포인터 변수의 참조형
data - type 포인터 변수명; data - type & 참조 변수명 = 포인터 변수명; |
▶ 참조 배열 : 배열의 참조형
data - type 배열명[n]; data - type (& 참조 변수명)[n] = 배열명; |
1
2
3
4
|
int age = 10;
int &num = age;
int *p1 = &age;
int *p2 = #
|
cs |
1
2
3
4
|
char message[] = "Hello, nice to meet you";
const char *str = message;
const char * &msg = str;
papa = "Nice to meet you, too";
|
cs |
1
2
3
4
5
6
|
const char *name = "홍길동";
int age = 30;
double height = 175.3;
profile(name, age, height);
void profile(const char* const &np, const int &a, const double &h) { ... }
|
cs |
1
2
3
4
5
6
|
void func(int x, int y);
void func(int &x, int &y);
int a = 10;
int b = 20;
fun(a, b);
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
using namespace std;
int &sub();
int num = 0;
int main(void) {
int res;
res = sub(); // res = num;
sub() = 10; // num = 10;
sub()++; // num++
return 0;
}
int &sub() {
num++;
return num;
}
|
cs |
[TIL] 1 - 7. 객체지향 기본 문법 - 동적메모리 할당 (0) | 2022.02.09 |
---|---|
[TIL] 1 - 5. 객체지향 기본 문법 - const의 이해 (0) | 2022.02.08 |
[TIL] 1 - 4. 객체지향 기본 문법 - C++에서 확장된 함수의 기능 (0) | 2022.02.08 |
[TIL] 1 - 3. 객체지향 기본 문법 - 네임스페이스 (0) | 2022.02.08 |
[TIL] 1 - 2. 객체지향 기본 문법 - C++에 추가된 데이터 타입 및 향상된 for문 (0) | 2022.02.07 |
댓글 영역