I have a widget in my GUI that displays chart(s). If I have more than one chart there will be a legend shown in a rectangle on the GUI.
I have a QStringlist (legendText)
which holds the text of the legend. If there is no legend required, legendText
would be empty. If there will be a legend, legendText
would hold the text.
For finding the height of the rectangle around the legend I would like to do the following:
int height = 10; QStringList legendText; ... height = height * (legendText->size() > 0); ...
Is this a good idea/ good style to multiply an int
with a boolean
? Will I run into problems with that?
Yes. It's safe to assume true is 1 and false is 0 when used in expressions as you do and is guaranteed: C++11, Integral Promotions, 4.5: An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.
The result of the multiplication of a float and an int is a float . Besides that, it will get promoted to double when passing to printf .
Nope, bools are designed to only store a 1 or a 0.
Treating integers as boolean values C++ does not really have a boolean type; bool is the same as int. Whenever an integer value is tested to see whether it is true of false, 0 is considered to be false and all other integers are considered be true.
This is technically fine, if a bit unclear.
The bool
will be promoted to an int
, so the result is well-defined. However, looking at that code I don't instantly get the semantics you are trying to achieve.
I would simply write something like:
height = legendText->isEmpty() ? 0 : height;
This makes your intent far clearer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With