标签:
C#运算符重载函数必须是public static的
struct CSTest
{
public int posx;
public static CSTest operator + (CSTest o1, CSTest o2)
{//二元运算符重载
CSTest ost = new CSTest();
ost.posx = o1.posx + o2.posx;
return ost;
}
public static CSTest operator - (CSTest ot)
{//一元运算符重载
ot.posx = -ot.posx;
return ot;
}
}
class Program
{
static void Main(string[] args)
{
CSTest ostn1 = new CSTest();
CSTest ostn2 = new CSTest();
ostn1.posx = 1;
ostn2.posx = 2;
CSTest ostn3 = ostn1 + ostn2;
ostn3 = -ostn3;
Console.WriteLine(ostn3.posx);
}
}
标签:
原文地址:http://www.cnblogs.com/timeObjserver/p/5928984.html