我希望将我的班级转换为双倍值。这可以通过重载double y = x
来实现,但这允许隐式转换,理想情况下我希望能够避免。
是否有任何方法可以添加此功能,但要求使用employee_array = [[1, 10, 9] [2, 19, 4] [3, 7, 6]]
employees = [Employee(*row) for row in employee_array]
进行转换,而不是隐式进行转换; xmlns:fcu="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
...
<Button fcu:Style="{StaticResource ButtonRevealStyle}" Grid.Column="1" Width="38" ... />
?
我正在使用C ++ 17。感谢
答案 0 :(得分:2)
是的,您可以在C ++ 11之后将转换运算符标记为显式。
try
这将阻止复制初始化,例如,
final String propertyUsedForSorting = "firstName";
Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
try {
Comparable prop1 = (Comparable) PropertyUtils.getProperty(o1, propertyUsedForSorting);
Comparable prop2 = (Comparable) PropertyUtils.getProperty(o2, propertyUsedForSorting);
return prop1.compareTo(prop2);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Could not resolve the property: " + propertyUsedForSorting, e);
} catch (ClassCastException e) {
throw new RuntimeException("Cannot use property " + propertyUsedForSorting + " for sorting.");
}
}
});
允许显式转换,例如
explicit operator double() { /* ... */ }
答案 1 :(得分:1)
使用double y(x);
double y = static_cast<double>(x);
:
sudo apt clean && sudo apt update