인프런 커뮤니티 질문&답변
초반에 코드가 이해가 안갑니다
작성
·
197
0
#include <iostream> #include<cstdlib> //rand(), srand() #include<ctime> //time() #include<fstream>//파일 스트리밍 using namespace std; class Cents { private: int m_cents; public: Cents(int cents = 0) { m_cents = cents; } int getCents() const { return m_cents; } int &getCents() { return m_cents; } friend bool operator == (const Cents &c1, const Cents &c2) { return c1.m_cents == c2.m_cents; } friend std::ostream &operator << (std::ostream &out, const Cents ¢s) { out << cents.m_cents; return out; } }; int main() { Cents cents1(6); Cents cents2(6); if (cents1 == cents2) { cout << "equal" << endl; } cout << std::boolalpha; return 0; }
디버그 해보면서 이해가 안가는게
if (cents1 == cents2)
{
cout << "equal" << endl;
}
이 부분으로 오면 바로
friend bool operator ==
(const Cents &c1, const Cents &c2)
이 함수로 가지던데 이유가 뭘까요??
if문에 == 마우스를 대보니 함수를 가르키던데
== 연산자가 friend bool operator == 이기 떄문인가요?
또 마지막 코드인 cout << std::boolalpha; 가 이해가 안가던데
디버그해도 그냥 바로 건너뛰고
지워도 문제가 안되던데 굳이 쓴 이유를 알고 싶습니다
감사합니다.
답변 1
1
1. 연산자 오버로딩한 부분으로 가는 것 맞습니다. 지금 비교 연산자 오버로딩 배우셨듯이 == 도 연산자 중 하나죠! 같은 챕터의 뒷부분에서 대입 연산자 오버로딩에 대해 배우시게 될 거에요.
2. 0, 1 정수를 false, true 로 출력하게끔 출력 방법을 설정해주는 코드입니다. 검색해보면 많은 자료가 나오니 확인 부탁드려요! https://woodforest.tistory.com/92





