c++ overloading << code example
Example 1: operator overloading in c++ <<
ostream &operator<<(ostream &output, const MyClass &myObject)
{
output << "P : " << myObject.property;
return output;
}
Example 2: c++ over load operator
vec2 operator - (vec2 const &other) {
return vec2(x - other.x, y - other.y);
}
Example 3: c++ overload < operator
struct Record
{
std::string name;
unsigned int floor;
double weight;
friend bool operator<(const Record& l, const Record& r)
{
return std::tie(l.name, l.floor, l.weight)
< std::tie(r.name, r.floor, r.weight);
}
};