반응형
이 둘은 클래스나 구조체에서 변수를 사용할때 이용하는 연산자이다.
즉 무언가에 접근 이라는 의미로 사용된다.
요약
1. .(도트) 는 클래스의 멤버를 직접적으로 접근한다.
2. ->(화살표) 는 포인터를 통해 멤버를 간접적으로 접근한다.
3. 즉 a->b 는 (*a).b 와 동일하다.
예시
- 아래 예시를 통해 이해해보자
#include <iostream>
using namespace std;
class Mycls {
public:
int age = 10;
int num = 1000;
const char* name = "홍길동";
};
struct Mystruct {
int age = 20;
int num = 2000;
const char* name = "김김김";
};
void main() {
Mystruct mystruct; //구조체 선언
// ->(화살표) 를 통해 구조체 포인터의 멤버에 접근한다
Mystruct* mystruct2 = new Mystruct; //구조체 동적할당
mystruct2->num = 3000;
// .(도트) 는 클래스의 멤버를 직접적으로 접근한다.
Mycls mycls; //클래스 선언
mycls.num = 4000;
Mycls* mycls2 = new Mycls(); //클래스 동적선언
cout << mystruct.num << endl; // 2000
cout << mystruct2->num << endl; // 3000
cout << mycls.num << endl; // 4000
cout << mycls2->num << endl; // 1000
}
끝
반응형
'공부 > 기타' 카테고리의 다른 글
[기타] AZ-900 시험 후기 (0) | 2023.01.29 |
---|---|
git diff 정리하기 (0) | 2022.10.09 |
Commitlint 커밋린트 란 (0) | 2022.09.06 |
colab에서 커스텀 폴더/패키지 import 방법 (0) | 2022.09.04 |
[Git에러] Password authentication is temporarily disabled as part of a brownout (0) | 2022.07.17 |
댓글