true 和 false 的运算符重载如何工作?
您可以重载运算符 true 和 false 我查看了示例并发现了这个 http: //msdn.microsoft.com/en-us/library/aa691312%28v=vs.71%29.aspx
我完全不明白它们是如何工作的。我知道如果我写 if(obj) 并且 true 返回 true 那么 if 就会被执行。什么错误返回并不重要。但是 false 是如何工作的呢?在该文档中,建议 &&操作员使用它。我写了下面的代码。我不知道如何获得 &&来编译。 ||也给我一个编译错误。我怎样才能被称为假的?我如何获得 &&和 ||去工作?
var ts= new MyTimeSpan();// ts += ts;
if (ts){ Console.WriteLine("Is true"); }
else { Console.WriteLine("not true"); }
//Console.WriteLine(ts && ts ? "Y" : "N");
class MyTimeSpan
{
public static MyTimeSpan operator +(MyTimeSpan t, MyTimeSpan t2) { return new MyTimeSpan(); }
public static bool operator true(MyTimeSpan t) { return true; }
public static bool operator false(MyTimeSpan t) { return false; }
}
You can overload operator true and false i looked at examples and found this http://msdn.microsoft.com/en-us/library/aa691312%28v=vs.71%29.aspx
I completely dont understand how they work. I know if i write if(obj) and true returns true then the if is executed. It doesnt matter what false returns. However how does false work? in that doc it is suggected that the && operator uses it. I wrote the code below. I dont know how to get && to compile. || gives me a compile error too. How do i get false to be called? and how do i get the && and || to work?
var ts= new MyTimeSpan();// ts += ts;
if (ts){ Console.WriteLine("Is true"); }
else { Console.WriteLine("not true"); }
//Console.WriteLine(ts && ts ? "Y" : "N");
class MyTimeSpan
{
public static MyTimeSpan operator +(MyTimeSpan t, MyTimeSpan t2) { return new MyTimeSpan(); }
public static bool operator true(MyTimeSpan t) { return true; }
public static bool operator false(MyTimeSpan t) { return false; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
短路运算符的定义属性是,如果左侧已经确定了结果,则不需要计算右侧。对于
or
,如果左侧为true
,则在任何情况下结果都将为true,并且无需评估右侧。对于和
来说,它是相同的,只是如果左侧为false
,则结果将为 false。您需要重载
|
和true
才能获取||
。并且&
和false
得到&&
。a||b
对应于op_true(a)?a:(a|b)
之类的内容。因此,如果 true 运算符返回 true,则不需要计算 b 的表达式。a&&b
对应于op_false(a)?a:(a&b)
之类的内容。因此,如果 false 运算符返回 true,则不需要计算 b 的表达式。创建自定义布尔类型(例如可为 null 的布尔值)时,重载短路运算符非常有用(请参阅
DBBool
)The defining property of a short circuiting operator is that it doesn't need to evaluate the right side if the left side already determines the result. For
or
if the left side istrue
the result will be true in any case and it's unnecessary to evaluate the right side. Forand
it's the same except that if the left side isfalse
the result will be false.You need to overload both
|
andtrue
to get||
. And&
andfalse
to get&&
.a||b
corresponds to something likeop_true(a)?a:(a|b)
. So if the true operator returns true it does not need to evaluate the expression of b.a&&b
corresponds to something likeop_false(a)?a:(a&b)
. So if the false operator returns true it does not need to evaluate the expression of b.Overloading the short circuiting operators is useful when creating a custom boolean type, such as nullable bools(See
DBBool
)重载
true
和false
运算符时,它们不仅仅返回true
和false
,它们还用于确定如果您的类型的值被认为是 true 或 false。例如,如果类中的零值表示 false,则非零值表示 true:
由于运算符彼此相反,因此编译器不需要使用两者来确定值是 true 还是 false。如果您编写
if(obj)
,编译器将使用true
运算符来确定该值是否为 true。When overloading the
true
andfalse
operators they don't just returntrue
andfalse
, they are used to determine if a value of your type is considered to be true or false.If for example a zero value in your class represents false, then a non-zero value represents true:
As the operators are each others inverse, the compiler doesn't need to use both to determine if a value is true or false. If you write
if(obj)
the compiler will use thetrue
operator to determine if the value is true.您将需要覆盖 & && 运算符和 |或||。
像这样的东西:(只是一个虚拟代码)
You will need to override & operator for && and | or ||.
Somthing like this: (Just a dummy code)