+ 1
Operator bool overloaded
Hi Never heard the term operator bool overload. I am aware about operator overloading for custom class but what this operator bool overloaded means? When it is executed for custom class and what is the need of that?
6 Respuestas
+ 3
overloading the operator bool in C++ allows a class to define how it should be evaluated in a boolean context, this pattern is frequently used in smart pointers, file handles, and other resource-managing classes to check if they contain valid data or resources.
+ 1
There is no bult in boolean data type like true or false in python, yeah but == and != operator can used as in the same way as bool and it's overloading is possible.
+ 1
technically " operator bool() " is not an overloaded operator in the traditional sense ( like +, -, *, = which define how an operator works with your class ), but is a type conversion operator (also called a conversion operator or cast operator) which define how your class can be automatically or explicitly converted to another type.
" What to do on object to call this bool operator? " :
https://www.sololearn.com/en/compiler-playground/c4qTPGYLNYYu
https://en.cppreference.com/w/cpp/language/cast_operator
0
1. What it does return ?
Return type operator bool()
2. It will not have any extra object as argument . Right ?
3. How to call this operator from class object?
For example, test obj;
What to do on obj object to call this bool operator?
0
You create your own class and create overloads of different operators. After all, the program does not know by what criteria you want to compare objects. Let's say that the string class is compared character by character, and if they are identical, true is returned.