Inflearn Community Q&A
오버로딩 syntax
Written on
·
190
0
안녕하세요! 갑자기 궁금증이 생겨서 질문 남깁니다. 왜 (), [], static cast 오버로딩을 할 때에는 앞에 friend가 안 붙는 건가요??
Quiz
사용자 정의 타입에 대해 연산자 오버로딩을 하는 주된 목적은 무엇일까요?
코드 실행 속도를 빠르게 하기 위해
사용자 정의 타입을 내장 타입처럼 자연스럽게 사용하기 위해
객체의 메모리 관리를 자동화하기 위해
클래스의 상속 관계를 정의하기 위해
Answer 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





