오버로딩은 다양한 연산자를 지원한다.
전위/후위 연산자
Foo& operator++()
{
cout << "pre" << endl;
return *this;
}
// 뒤에 int는 컴파일러가 후위 증가 연산자를 전위 증가 연산자와 구별하기 위해 사용된다.
Foo operator++(int)
{
cout << "post" << endl;
return *this;
}
#include <iostream>
#include <utility>
struct Foo
{
int n;
};
bool operator==(const Foo& lhs, const Foo& rhs)
{
return lhs.n == rhs.n;
}
bool operator<(const Foo& lhs, const Foo& rhs)
{
return lhs.n < rhs.n;
}
int main()
{
Foo f1 = {1};
Foo f2 = {2};
using namespace std::rel_ops;
std::cout << std::boolalpha
<< "{1} != {2} : " << (f1 != f2) << '\\n'
<< "{1} > {2} : " << (f1 > f2) << '\\n'
<< "{1} <= {2} : " << (f1 <= f2) << '\\n'
<< "{1} >= {2} : " << (f1 >= f2) << '\\n';
}
struct Foo
{
Foo(double a)
: z(a)
{
}
explicit operator double() const
{
cout << "double" << endl;
return z;
}
Foo& operator+(const Foo&)
{
cout << "operator+" << endl;
return *this;
}
double z;
};
int main()
{
Foo foo { 6.6 };
double a { foo + 3.3 };
return 0;
}