Inflearn Community Q&A
friend bool operator < ()에서 질문 있습니다.
Written on
·
259
0
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 bool operator != (const Cents &c1, const Cents &c2)
{
return bool (c1.m_cents != c2.m_cents);
}
friend std::ostream& operator << (std::ostream &out, const Cents ¢s)
{
out << cents.m_cents;
return out;
}
};
강의에서 이와 같이 구현을 합니다. 여기서 질문이 있습니다. bool이 붙은 friend 함수들은 Cents class의 멤버 함수로 바꿀 수가 없나요? 두가지 시도를 했는데 오류가 났습니다.
1. Cents bool operator <()
2. bool Cents operator <()
Answer 2
0
0
오류가 나는 이유는 파라미터가 없어서입니다.
비교연산자이니만큼 당연 비교할 수 있는 대상이 있어야합니다. 그래서 매개변수가 있어야 합니다.
예를 들면 이런식입니다.
bool operator < (const Cents &c2)
(매개변수로 들어오는 Cents 타입의 타객체와 비교)






앗... 제가 끝까지 안적어서 혼동을 드린 것 같습니다.
1. Cents bool operator <(const Cents &c1, const Cents &c2)
2. bool Cents operator <(const Cents &c1, const Cents &c2)
이 부분에서 () 안에는 매개변수가 들어있는 상황입니다. 생략을 해버려서 혼동을 드렸네요.
다시 질문드리겠습니다. ㅠㅠ
bool이 붙은 friend 함수들은 Cents class의 멤버 함수로 바꿀 수가 없나요?