Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task: .과 화살표 #70

Closed
fkdl0048 opened this issue Oct 6, 2024 · 0 comments · Fixed by #74
Closed

Task: .과 화살표 #70

fkdl0048 opened this issue Oct 6, 2024 · 0 comments · Fixed by #74
Assignees
Labels

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 6, 2024

. vs ->

C언어를 많이 다뤄봤다면 크게 헷갈리지 않지만, 포인터의 개념과 레퍼런스등의 개념을 배우게 되면서 조금 헷갈릴 수 있는 개념이다.

.연산자는 객체 자체를 통해 멤버에 접근할 때 사용한다.

struct Person {
    std::string name;
    int age;
};

int main() {
    Person person;
    person.name = "Alice";
    person.age = 30;

    std::cout << person.name << std::endl;
    return 0;
}

->연산자는 객체에 대한 포인터를 통해 멤버에 접근할 때 사용한다.

struct Person {
    std::string name;
    int age;
};

int main() {
    Person* personPtr = new Person;
    personPtr->name = "Bob";
    personPtr->age = 25;

    std::cout << personPtr->name << std::endl;

    delete personPtr;
    return 0;
}

차이점은 .은 객체의 실제 인스턴스를 통해 멤버에 접근한다는 것이고, 화살표는 객체의 포인터를 통해 멤버에 접근한다는 것이다.

사실 ->를 사용하는 이유는 포인터를 역참조한 후 점 연산자를 사용하는 것과 같다.

personPtr->name = "Bob";
(*personPtr).name = "Bob";

추가로 레퍼런스의 경우엔 실제 객체와 같이 사용되므로 .연산자만 사용할 수 있다.

struct Person {
    std::string name;
    int age;
};

int main() {
    Person person;
    Person& personRef = person;     // 레퍼런스
    Person* personPtr = &person;    // 포인터

    // 레퍼런스 사용 시
    personRef.name = "Charlie";
    personRef.age = 28;

    // 포인터 사용 시
    personPtr->name = "Dave";
    personPtr->age = 35;

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant