Professional Documents
Culture Documents
When operators such as && , || are overloaded, they lose their special properties of
short-circuit evaluation and sequencing.
They will be misused. Programmers forget or don't know the short-circuit evaluation
case for operator overloading.
resolves as 4 + (3 ) = 4 + 9 = 13
2
However, in C++, the arithmetic operators have higher precedence than operator^ , so
4 + 3 ^ 2 resolves as (4 + 3) ^ 2 => 7 ^ 2 => 49 .
4 + (3 ^ 2) every time you used it for this to work properly, which isn’t intuitive, and
is potentially error-prone.
Because of this precedence issue, it’s generally a good idea to use operators only in an
analogous way to their original intent.
Rule: When overloading operators, it’s best to keep the function of the operators as
close to the original intent of the operators as possible.
https://www.learncpp.com/cpp-tutorial/91-introduction-to-operator-overloading/
Fraction
Observe that: copy constructors and operators can directly access private variables of
another object of same class.
https://stackoverflow.com/questions/4117002/why-can-i-access-private-variables-
in-the-copy-constructor
Code Reusability
○ The ability to reuse the existing coding efforts for a new function instead of
duplicating efforts.
Inheritance in practice
● 3 types of programmers
● Tips:
○ Don’t extend a base class that was not designed for extension
● Reference and pointer derived object:
Once declared virtual, any derived is virtual, even if keyword not used
Non-Virtual: call based on the the used pointer type (compile-time / static binding)
■ Can’t also use as a function parameter or return as object, but can as pointer
● Downcasting
○ Static cast if confident. Otherwise: Dynamic cast and check the returned pointer
● Work more with interfaces, sometimes Abstract classes and much less with multiple
inheritance ⇒ avoids inheritance troubles
Interfaces: Guidelines
● Interface = contract. Don’t break it.
Interfaces as properties
relevant functionalities
Polymorphism: Misc
● Virtual Tables
○ Extra pointer in base class + table per class: #rows = #virtual functions
○ Computed In compile time: When compiling we know all classes and their
relationships
specific time part of some cycle. Polymorphism may help designing that
● Make sure every class has a single responsibility: single reason to change
● A non-virtual function
● A virtual function
● Cons
● Can be overloaded:
○ + - / % ^ & | ~ !, = = ++ -- == != && || += -= /=
○ [] = -> ()
○ . . :: ?: sizeof
● Your approach
○ Or: Define as possible as member functions + use non-member for remaining cases
● Extensibility
○ As you see, operator overloading allows adding extra functionalities for a class
○ E.g. f1() + f2() + f3(). There are 6 ways of evaluating this expression
● Since C++17:
○ Evaluation of overloaded operators are now sequenced in the same way as for built-in
operators
○ the <<, >> and subscripting operators now have the left operand sequenced before the
right
Operators Summary
● Common ones == = < << >> + [Don’t || && ^ and probably many others]
○ Think twice in what you are operator overloading. Don’t shoot yourself in the foot
○ So one may need more code tracing & debugging. Don’t make people life harder.
Operators Summary
● Properties
○ Extensibility
○ Arity: Some operators are unary, others are binary and some both
● Reading