(리턴 타입) operator(연산자) (연산자가 받는 인자)
bool operator==(MyString& str);
bool MyString::operator==(MyString& str) {
return !compare(str); // str 과 같으면 compare 에서 0 을 리턴한다.
}
int main() {
MyString str1("a word");
MyString str2("sentence");
MyString str3("sentence");
if (str1 == str2)
std::cout << "str1 와 str2 같다." << std::endl;
else
std::cout << "st1 와 str2 는 다르다." << std::endl;
if (str2 == str3)
std::cout << "str2 와 str3 는 같다." << std::endl;
else
std::cout << "st2 와 str3 는 다르다" << std::endl;
}
연산자를 통해서 위와같이 사용이 가능하다!!!
operator를 이용할 때, 연산자 오버로딩을 하게 된다면 모든 연산자들에 대해 개별적인 정의가 필요하다.