重载自增操作符

飞阁 2008-07-16 10:08:07
c++中可以分别重载前缀和后缀的自增操作符,但是c#中应该怎么重载呢?
...全文
58 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞阁 2008-07-17
  • 打赏
  • 举报
回复
恩,我试过了,是会自己区别的。谢谢了
aimeast 2008-07-16
  • 打赏
  • 举报
回复
前几天看完书,书上说没有方法。
貌似是直接定义就行了,编译器会区别前缀后缀的。
飞阁 2008-07-16
  • 打赏
  • 举报
回复
我不是问怎么重载操作符,是问重载前缀和后缀的自增操作符
smntbk 2008-07-16
  • 打赏
  • 举报
回复
参考:
public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary) //constructor
{
this.real = real;
this.imaginary = imaginary;
}

// Declare which operator to overload (+),
// the types that can be added (two Complex objects),
// and the return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

// Override the ToString() method to display a complex number in the traditional format:
public override string ToString()
{
return (System.String.Format("{0} + {1}i", real, imaginary));
}
}


grearo 2008-07-16
  • 打赏
  • 举报
回复
项目右键-属性-生成-勾选允许不安全代码
system_002 2008-07-16
  • 打赏
  • 举报
回复
up

111,094

社区成员

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

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

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