【发布时间】:2022-08-10 09:15:31
【问题描述】:
我想知道如何简化如下陈述。
我到处都有类似的代码,并想清除它。
if(isActive)
{
if(columnId == 4)
g.drawText(active[row].value, 2, 0, width, height, Justification::centredLeft, true);
}
else
{
if(columnId == 4)
g.drawText(inactive[row].value, 2, 0, width, height, Justification::centredLeft, true);
}
isActive,你可以想象,是一个bool 值。
-
if(columnId == 4) { g.drawText(isActive ? active[row].value : inactive[row].value, ...); }? -
auto value_to_pass = isActive? active[row].value : inactive[row].value; g.drawText(value_to_pass, ...); -
假设
active和inactive具有相同的类型......if (column[Id == 4) {auto thing = (isActive ? active : inactive)[row].value; g.drawText(thing, 2, 0, width, height, Justification::centred);}甚至if (columnID == 4) g.drawText((isActive : active : inActive)[row].value, 2, 0, width, height, Justification::centred);。不过,有些人会争论它的可读性。
标签: c++ c++11 c++17 coding-style