我想问一下,在c#中关于++运算符得重载,我只见到过++的前缀版本,不知道它的后缀版本是如何实现的.还请大虾指教阿。
using System;
class Point
{
public int x;
public int y;
public static Point operator ++ ( Point LValue )
{
Point newpoint = new Point();
newpoint.x = RValue.x +1;
newpoint.y =RValue.y +1;
return newpoint;
}
public static Point operator ++ ( Point LValue )
{//后缀如何写啊
}
public static void Main()
{
Point mypoint = new Point();
mypoint.x = 100;
mypoint.y= 200;
Console.WriteLine(mypoint.x);
Console.WriteLine (mypoint.y);
mypoint =mypoint++;
Console.WriteLine (mypoint.x);
Console.WriteLine (mypoint.y);
}
}
you only need one version, see the specification:
http://www.jaggersoft.com/csharp_standard/14.6.5.htm
"......
The ++ and --operators also support postfix notation (§14.5.9). 2 The result of x++ or x--is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation. 3 In either case, x itself has the same value after the operation.
Paragraph 6
An operator ++ or operator --implementation can be invoked using either postfix or prefix notation.
It is not possible to have separate operator implementations for the two notations.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...."