
위 둘은 클래스나 구조체에서 멤버변수를 사용할 때 이용하는 연산자들 이다.
즉, 무언가를 접근할 때 사용하는 연산자로 쓰인다.
아래에 예시를 보자.
#include<iostream>
using namespace std;
class Account {
public:
const char* name = "홍길동";
const char* Account_Number = "293-685-233-574639";
int balance = 9870;
};
struct User {
public:
const char* UserName = "돈내놔";
const char* Job = "전사";
int Level = 58;
};
int main(void) {
// MyClass 클래스선언
Account MyClass;
// .(도트)는 클래스의 맴버에 직접적으로 접근할 수 있다.
cout << MyClass.name << endl;
cout << MyClass.Account_Number << endl;
cout << MyClass.balance << endl;
// MyUser 구조체선언
User* MyUser = new User();
// ->(화살표)는 구조체의 멤버에 간접적으로 접근할 수 있다.
cout << MyUser->UserName << endl;
cout << MyUser->Job << endl;
cout << MyUser->Level << endl;
delete MyUser;
return 0;
}
포인터 객체인데, 도트연산자( . )를 사용한다거나,
포인터객체가 아닌데 화살표 연산자( -> )를 사용하면 에러가 발생한다.
만약 본인이 정말 도트연산자를 사용하고 싶다면
MyUser -> UserName
다음과 같이 사용하면 된다.
(*MyUser).UserName