mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 19:24:36 +03:00
@@ -332,7 +332,7 @@ They should be preferred to macros, because macros do not honor namespaces, etc.
|
|||||||
|
|
||||||
## Use Operator Overloads Judiciously
|
## Use Operator Overloads Judiciously
|
||||||
|
|
||||||
Operator overloading was invented to enable expressive syntax. Expressive in the sense that adding two big integers looks like `a + b` and not `a.add(b)`. Another common example is std::string, where it is very common to concatenate two strings with `string1 + string2`.
|
Operator overloading was invented to enable expressive syntax. Expressive in the sense that adding two big integers looks like `a + b` and not `a.add(b)`. Another common example is `std::string`, where it is very common to concatenate two strings with `string1 + string2`.
|
||||||
|
|
||||||
However, you can easily create unreadable expressions using too much or wrong operator overloading. When overloading operators, there are three basic rules to follow as described [on stackoverflow](http://stackoverflow.com/questions/4421706/operator-overloading/4421708#4421708).
|
However, you can easily create unreadable expressions using too much or wrong operator overloading. When overloading operators, there are three basic rules to follow as described [on stackoverflow](http://stackoverflow.com/questions/4421706/operator-overloading/4421708#4421708).
|
||||||
|
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ if (caseA) {
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// Better Idea
|
// Better Idea
|
||||||
const std::string somevalue = caseA?"Value A":"Value B";
|
const std::string somevalue = caseA ? "Value A" : "Value B";
|
||||||
```
|
```
|
||||||
|
|
||||||
More complex cases can be facilitated with an [immediately-invoked lambda](http://blog2.emptycrate.com/content/complex-object-initialization-optimization-iife-c11).
|
More complex cases can be facilitated with an [immediately-invoked lambda](http://blog2.emptycrate.com/content/complex-object-initialization-optimization-iife-c11).
|
||||||
|
|||||||
Reference in New Issue
Block a user