我可以在C ++中将int与布尔值相乘吗? [英] Can I multiply an int with a boolean in C++?

查看:157
本文介绍了我可以在C ++中将int与布尔值相乘吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GUI中有一个显示图表的小部件.如果我有多个图表,则GUI上的矩形中会显示一个图例.

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.

我有一个QStringlist (legendText),其中保存了图例的文本.如果不需要图例,legendText将为空.如果会有图例,则legendText将保留文本.

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);
 ...

intboolean相乘是一个好主意/好的风格吗?我会遇到问题吗?

Is this a good idea/ good style to multiply an int with a boolean? Will I run into problems with that?

推荐答案

如果尚不清楚,从技术上讲这很好.

This is technically fine, if a bit unclear.

bool将被提升int,因此结果是明确定义的.但是,查看该代码后,我无法立即获得您想要实现的语义.

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.

我会简单地写:

height = legendText->isEmpty() ? 0 : height;

这使您的意图更加清晰.

This makes your intent far clearer.

这篇关于我可以在C ++中将int与布尔值相乘吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆