Skip to content

Commit 27533f6

Browse files
committed
learncpp introduce overloading operators
1 parent 4a38abd commit 27533f6

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

content/posts/cpp-learn.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,3 +3875,83 @@ Name is a Derived
38753875

38763876
### 21.8. Printing inherited classes using operator<<
38773877
- Refer to the learncpp.com
3878+
3879+
## 22. Overloading Operators
3880+
- Almost any existing operator in C++ can be overloaded. The exceptions are:` conditional (?:), sizeof, scope (::), member selector (.), pointer member selector (.*), typeid, and the casting operators`.
3881+
- We can only overload the operators that exist.
3882+
- At least one of the operands in an overloaded operator must be a user-defined type.
3883+
3884+
- Overloading the plus operator (+) is as simple as declaring a function named `operator+`, giving it two parameters of the type of the operands we want to add, picking `an appropriate return type`, and then writing the function.
3885+
3886+
- Not everything can be overloaded as a friend function: The assignment (=), subscript ([]), function call (()), and member selection (->) operators must be overloaded as member functions, because the language requires them to be.
3887+
3888+
> The following rules of thumb can help you determine which form is best for a given situation:
3889+
If you’re overloading assignment (=), subscript ([]), function call (()), or member selection (->), do so as a member function.
3890+
If you’re overloading a unary operator, do so as a member function.
3891+
If you’re overloading a binary operator that does not modify its left operand (e.g. operator+), do so as a normal function (preferred) or friend function.
3892+
If you’re overloading a binary operator that modifies its left operand, but you can’t add members to the class definition of the left operand (e.g. operator<<, which has a left operand of type ostream), do so as a normal function (preferred) or friend function.
3893+
If you’re overloading a binary operator that modifies its left operand (e.g. operator+=), and you can modify the definition of the left operand, do so as a member function.
3894+
3895+
- e.g.
3896+
```cpp
3897+
#include <iostream>
3898+
3899+
class Cents
3900+
{
3901+
private:
3902+
int m_cents {};
3903+
3904+
public:
3905+
Cents(int cents) : m_cents{ cents } { }
3906+
3907+
// add Cents + Cents using a friend function
3908+
friend Cents operator+(const Cents& c1, const Cents& c2);
3909+
3910+
// 3. Using member function
3911+
// Overload Cents + int
3912+
Cents operator+(int value) const;
3913+
3914+
int getCents() const { return m_cents; }
3915+
};
3916+
3917+
// 1. Using friend function note: this function is not a member function!
3918+
Cents operator+(const Cents& c1, const Cents& c2)
3919+
{
3920+
// use the Cents constructor and operator+(int, int)
3921+
// we can access m_cents directly because this is a friend function
3922+
return c1.m_cents + c2.m_cents;
3923+
}
3924+
3925+
// 2. Using normal function
3926+
// note: this function is not a member function nor a friend function!
3927+
Cents operator-(const Cents& c1, const Cents& c2)
3928+
{
3929+
// use the Cents constructor and operator+(int, int)
3930+
// we don't need direct access to private members here
3931+
return { c1.getCents() - c2.getCents() };
3932+
}
3933+
3934+
// 3. Using member function
3935+
// note: this function is a member function!
3936+
// the cents parameter in the friend version is now the implicit *this parameter
3937+
Cents Cents::operator+ (int value) const
3938+
{
3939+
return Cents { m_cents + value };
3940+
}
3941+
3942+
int main()
3943+
{
3944+
Cents cents1{ 6 };
3945+
Cents cents2{ 8 };
3946+
Cents centsSum{ cents1 + cents2 };
3947+
std::cout << "I have " << centsSum.getCents() << " cents.\n";
3948+
3949+
Cents centsSub{ cents1 - cents2 };
3950+
std::cout << "I have " << centsSub.getCents() << " cents.\n";
3951+
3952+
const Cents cents3 { cents1 + 2 };
3953+
std::cout << "I have " << cents3.getCents() << " cents.\n";
3954+
3955+
return 0;
3956+
}
3957+
```

0 commit comments

Comments
 (0)