自增运算符重载的问题

rpx914 2009-04-11 03:43:35

using System;
namespace ConsoleApplication1
{
public class ComplexType
{
public double r1;
public double r2;
public ComplexType(double R1, double R2)
{
r1 = R1;
r2 = R2;
}
public static ComplexType operator ++(ComplexType test)
{
test.r1++;
test.r2++;
return test;
}
}
class Program
{
static void Main(string[] args)
{
ComplexType test = new ComplexType(2, 3);
(++test)++;//这句抛出了一个异常
Console.WriteLine("R1 = {0}\n" + "R2 = {1}", test.r1, test.r2);
}
}
}


在C++中
public static ComplexType operator ++(ComplexType test)
应写为
public static ComplexType& operator ++(ComplexType test)
这样(test++)++才不会报错
在C#中应该怎样写才能让(++test)++不抛出异常?
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rpx914 2009-04-11
  • 打赏
  • 举报
回复
囧了,真是这样。。。
vrhero 2009-04-11
  • 打赏
  • 举报
回复
这属于语法错误...和运算符重载没有关系...
LemIST 2009-04-11
  • 打赏
  • 举报
回复
测试一下这个:
int i = 0;
(++i)++;

同样有错,可见没有办法了.

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧