작성
·
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