官术网_书友最值得收藏!

Notes on operator overloading

C++ provides a powerful mechanism for overloading operators for custom types. It's much better to calculate the sum of two objects using the + operator, rather than calling a member function. Calling a member function also involves remembering its name before calling it. It might be add, calculateSum, calculate_sum, or something else. Operator overloading allows for a consistent approach in class design. On the other hand, overloading operators increases unnecessary verbosity in the code. The following snippet represents a list of comparison operators being overloaded, along with addition and subtraction for the Money class:

constexpr bool operator<(const Money& a, const Money& b) { 
return a.value_ < b.value_;
}
constexpr bool operator==(const Money& a, const Money& b) {
return a.value_ == b.value_;
}
constexpr bool operator<=(const Money& a, const Money& b) {
return a.value_ <= b.value_;
}
constexpr bool operator!=(const Money& a, const Money& b) {
return !(a == b);
}
constexpr bool operator>(const Money& a, const Money& b) {
return !(a <= b);
}
constexpr bool operator>=(const Money& a, const Money& b) {
return !(a < b);
}
constexpr Money operator+(const Money& a, const Money& b) {
return Money{a.value_ + b.value_};
}
constexpr Money operator-(const Money& a, const Money& b) {
return Money{a.value_ - b.value_};
}

As you can see, most of the preceding functions directly access the value member of the Money instance. To make it work, we should declare them as friends for Money. Here's what Money will look like:

class Money
{
public:
Money() {}
explicit Money(double v) : value_{v} {}
// construction/destruction functions omitted for brevity

public:
friend constexpr bool operator<(const Money&, const Money&);
friend constexpr bool operator==(const Money&, const Money&);
friend constexpr bool operator<=(const Money&, const Money&);
friend constexpr bool operator!=(const Money&, const Money&);
friend constexpr bool operator>(const Money&, const Money&);
friend constexpr bool operator>=(const Money&, const Money&);
friend constexpr bool operator+(const Money&, const Money&);
friend constexpr bool operator-(const Money&, const Money&);

private:
double value_;
};

The class looks monstrous. C++20 introduces the spaceship operator, which allows us to skip the definition of comparison operators. operator<=>(), also known as the three-way comparison operator, requests the compiler to generate relational operators. For the Money class, we can use the default operator<=>(), as shown here:

class Money
{
// code omitted for brevity
friend auto operator<=>(const Money&, const Money&) = default;
};

The compiler will generate the ==, !=, <, >, <=, >= operators. The spaceship operator reduces the redundant definitions for operators and also provides a way to implement a generic behavior for all the generated operators. When implementing a custom behavior for the spaceship operator, we should note the return value type of the operator. It can be one of the following:

  • std::strong_ordering
  • std::weak_ordering
  • std::partial_ordering
  • std::strong_equality
  • std::weak_equality

All of them are defined in the <compare> header. The compiler generates operators based on the return type of the three-way operator. 

主站蜘蛛池模板: 商水县| 普洱| 建德市| 新平| 扎鲁特旗| 满城县| 静乐县| 盐池县| 江津市| 新昌县| 安溪县| 瑞昌市| 独山县| 涡阳县| 深水埗区| 沾化县| 杭锦旗| 商丘市| 班玛县| 清河县| 五大连池市| 贵州省| 北川| 德州市| 华容县| 双峰县| 富顺县| 泸溪县| 长丰县| 文化| 江都市| 壶关县| 景谷| 浙江省| 乐亭县| 府谷县| 迁安市| 平度市| 乌恰县| 建始县| 上饶市|