https://www.learncpp.com/cpp-tutorial/member-selection-with-pointers-and-references/
앞서 배운바에 의하면 struct의 member variable에 object에서 접근하기 위해서
(.) operator를 활용해 접근했다
그런데 만약 struct point type의 varibale에서 member에 어떻게 접근할까
(*ptr).id와 같이 dereference를 활용해서 접근할 수도 있지만 not great
member selection from pointer operator (->) 를 활용해 접근해서 사용하자
(다른 말로 arrow opertaor)
#include <iostream>
struct Employee
{
int id{};
int age{};
double wage{};
};
int main()
{
Employee joe{ 1, 34, 65000.0 };
++joe.age;
joe.wage = 68000.0;
Employee* ptr{ &joe };
std::cout << ptr->id; // Better: use -> to select member from pointer to object
return 0;
}
하나의 예시로 다음과 같이 사용할 수 있따
#include <iostream>
#include <string>
struct Paw
{
int claws{};
};
struct Animal
{
std::string name{};
Paw paw{};
};
int main()
{
Animal puma{ "Puma", { 5 } };
Animal* ptr{ &puma };
// ptr is a pointer, use ->
// paw is not a pointer, use .
std::cout << ptr->paw.claws << '\n';
return 0;
}
-> 와 . 을 혼용해서 접근하고 있다