강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

AJ Moon님의 프로필 이미지
AJ Moon

작성한 질문수

홍정모의 따라하며 배우는 C++

9.8 형변환을 오버로딩 하기

오버로딩 syntax

작성

·

173

0

안녕하세요! 갑자기 궁금증이 생겨서 질문 남깁니다. 왜 (), [], static cast 오버로딩을 할 때에는 앞에 friend가 안 붙는 건가요??

답변 2

1

홍정모님의 프로필 이미지
홍정모
지식공유자

강의에서 말로 설명드린 부분인 것 같은데 강의가 많다보니 어디인지 기억이 안나서 교재를 다시 찾아봤습니다.

아래는 friend를 써야하는 이유인데요, 그 외의 경우에는 friend를 쓰지 않아도 되겠지요.

https://www.learncpp.com/cpp-tutorial/94-overloading-operators-using-member-functions/

However, we are not able to overload operator<< as a member function. Why not? Because the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library. We can’t modify the class declaration to add the overload as a member function of std::ostream.

This necessitates that operator<< be overloaded as a friend.

조금 더 아래에 보면

When to use a normal, friend, or member function overload

항목 아래에 설명이 더 있습니다. 혹시 번역에 어려움이 느껴지시면 다시 질문해주세요!

0

AJ Moon님의 프로필 이미지
AJ Moon
질문자

답변 감사드립니다! 미국에서 유학중이라 오히려 영어로 보니 내용이 조금 더 익숙해서 이해가 되네요!

AJ Moon님의 프로필 이미지
AJ Moon

작성한 질문수

질문하기