- Expert C++
- Vardan Grigoryan Shunguang Wu
- 296字
- 2021-06-24 16:34:02
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.
- The Modern C++ Challenge
- Flink SQL與DataStream入門、進(jìn)階與實(shí)戰(zhàn)
- 假如C語言是我發(fā)明的:講給孩子聽的大師編程課
- Mastering Python High Performance
- Python:Master the Art of Design Patterns
- 蘋果的產(chǎn)品設(shè)計(jì)之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗(yàn)的七個原則
- 監(jiān)控的藝術(shù):云原生時代的監(jiān)控框架
- Mastering Bootstrap 4
- Mastering Python
- 劍指大數(shù)據(jù):企業(yè)級電商數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)(精華版)
- Lync Server Cookbook
- Python GUI設(shè)計(jì)tkinter菜鳥編程(增強(qiáng)版)
- Visual FoxPro程序設(shè)計(jì)
- Python機(jī)器學(xué)習(xí)
- 面向?qū)ο蠹夹g(shù)與工具(第2版)