将我的班级转换为Int64,Double等 [英] Casting my Class to Int64, Double etc
本文介绍了将我的班级转换为Int64,Double等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个名为MyDouble
的类,看起来像这样
I've created a class called MyDouble
looks like this
class MyDouble
{
double value;
//overloaded operators and methods
}
我能够在MyDouble上进行各种操作.例子:
I am able to do all kinds of operations on MyDouble. Examples:
MyDouble a = 5.0;
a += 3.0;
...etc
但是,这仍然会引发错误
However, this still throws an error
MyDouble a = 5.0;
long b = (Int64)a; //error
long b = (int64)a.value; //works
如何定义它,以便像(Int64)a
这样的操作自动转换为(Int64)a.value
?我不想让用户不必担心value
属性的存在.
How can I define it such that an operation like (Int64)a
automatically converts to (Int64)a.value
? I don't want the user to ever have to worry about the existence of the value
property.
推荐答案
In order for this conversion to work, you would need an explicit conversion to Int64.
这看起来像:
class MyDouble
{
double value;
public static explicit operator Int64(MyDouble value)
{
return (Int64)value.value;
}
}
这篇关于将我的班级转换为Int64,Double等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文