C# 运算符重载后置增量
我正在编写一个日期类,并且在修复后增量方面遇到问题(前缀增量似乎很好)。
以下是示例代码:
public class date
{
int year,
month,
day;
public date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
static public date operator ++(date d)
{
return d.Next(d);
}
}
“Next(date d)”方法接受一个日期并返回明天的日期(为了简洁起见,我将其省略)。我对 C# 还太年轻,无法理解为什么前缀很好但后缀增量没有任何作用。但请记住,在 C++ 中,我们必须有两种方法,而不仅仅是一种 - 用于前缀和后缀增量。
编译时也没有错误或警告。
I'm coding a date class and am having trouble with the post-fix increment (the prefix increment seems fine).
Here is the sample code:
public class date
{
int year,
month,
day;
public date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
static public date operator ++(date d)
{
return d.Next(d);
}
}
The method "Next(date d)" takes a date and returns tomorrows date (I left it out for brevity). I'm to young in C# to understand why the prefix is fine but postfix increment does nothing. But remember in C++ we would have to have two methods instead of just one - for prefix and postfix increments.
Also no errors or warnings on compile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
System.DateTime.AddDays
为自己保存一个史诗般的日期为主的头痛。
System.DateTime.AddDays
Save yourself an epic, date-based headache.
好吧,您还没有展示
Next
方法,这会有点方便......特别是展示为什么它需要将date
作为参数。我的猜测是您的Next
方法有缺陷。您也没有显示后增量失败的示例。下面是一个简化的示例,表明它确实有效:
注意它如何打印 2,表明日期已增加(或者更确切地说,
x
现在指的是Date
具有递增的日期值)。就我个人而言,我不认为我会为
Date
类引入 ++ 运算符,但没关系。我还建议构造函数应该是年/月/日而不是日/月/年;这是更传统的方法,并且更适合您希望通过更多参数获得更高精度的情况。Well you haven't shown the
Next
method, which would be kinda handy... in particular showing why it needs to take adate
as an argument. My guess is that yourNext
method is flawed.You also haven't shown an example of it failing for postincrement. Here's a simplified example which shows that it does work:
Note how it prints 2, showing that the day has been incremented (or rather,
x
now refers to a new instance ofDate
which has an incremented day value).Personally I don't think I'd introduce a ++ operator for a
Date
class anyway, but never mind. I'd also suggest that the constructor should be year/month/day rather than day/month/year; that's more conventional, and fits in better with situations where you want to allow more precision with more parameters.乔恩谢谢,你说得完全正确,让我附加类中缺少的 next() 方法:
由于它使用 valid() 我也会附加它:
Day()、Month() 和 Year() I认为是不言自明的,但如果需要的话请告诉我。我还有一个 previous() 方法,它与 next() 相反,我想在 -- decrement 方法中使用它。
现在在我的程序中,我有
所以它实际上并没有失败,它只是打印今天的日期而不是明天的日期,但如果我使用 ++today 则可以正常工作。
关于 D/M/Y 的顺序,是的,我同意,通过更高频率的数据,我可以看到这如何改进事情,接下来我将继续解决这个问题。
Jon thanks, you're absolutely right let me attach the missing next() method that is inside the class:
Since this uses valid() I'll attach this also:
the Day(), Month(), and Year() I think are self explanatory but let me know if they're needed. I also have a previous() method that does the opposite of next() which I want to use in the -- decrement method.
Now in my program, I have
So it doesn't actually fail it just prints todays date instead of tomorrow's but works correctly if I had used ++today instead.
Regarding the order D/M/Y, yep I agree, with higher frequency data I can see how that improves things, I'll move on to fixing that next.
您可以添加的日期或月份/年份
what ever may be the day or month/yr you can add
我还有一条附加评论,对于原始海报来说可能为时已晚,但可能对将来阅读的任何人都有用。
看看你的“有效”实现:
它将在 29/2/2100 失败,这是一个有效日期,但你的方法说它不是。
I have an additional comment that is probably too late for the original poster, but may be useful to anyone reading in the future.
Looking at your implementation of "valid":
It will fail on 29/2/2100, which is a valid date but your method says it's not.