C# 中的结构真正需要多少次重写?

发布于 2024-11-19 02:04:41 字数 1642 浏览 9 评论 0原文

我正在为模拟器项目中的“单词”制作包装器。它的目的是把我所有的演员转换都放在一个地方。我正要开始实现数学函数的所有覆盖(+、-、/、8、移位等),当我想到所有隐式函数不应该处理这个问题吗?当我得到 > 时,我需要超越 >= 和 <= 吗? <和==?

我想我会问这个问题,因为虽然有很多关于如何创建它们的问题,但关于多少才足够的问题却很少。下面是代码:

public struct word_t
{
    ulong val;
    word_t(ulong val) { this.val = val; }
    public static implicit operator word_t(int a) { return new word_t((ulong)a); }
    public static implicit operator word_t(long a) { return new word_t((ulong)a); }
    public static implicit operator word_t(uint a) { return new word_t((ulong)a); }
    public static implicit operator word_t(ulong a) { return new word_t((ulong)a); }
    public static implicit operator int(word_t a) { return (int)a.val; }
    public static implicit operator long(word_t a) { return (long)a.val; }
    public static implicit operator uint(word_t a) { return (uint)a.val; }
    public static implicit operator ulong(word_t a) { return (ulong)a.val; }
    public static bool operator ==(word_t a, word_t b) { return a.val == b.val; }
    public static bool operator !=(word_t a, word_t b) { return a.val != b.val; }
    public static bool operator >(word_t a, word_t b) { return a.val > b.val; }
    public static bool operator <(word_t a, word_t b) { return a.val < b.val; }
    public override bool Equals(object obj) {
            return obj.Equals(val);
    }
    public override int GetHashCode() {
        return val.GetHashCode();
    }
    public override string toString() {
        return val.ToString();
    }
}

我的直觉告诉我“相信编译器”,但我的头脑总是担心它的效率如何。

PS 我刚刚意识到我应该覆盖移位,因为负数问题的位移位,但现在想象一下移位就像 uint 和 int 之间的加法一样神奇地工作。

I am making a wrapper for a "word" in an emulator project. Its meant to put all my cast conversions all in one spot. I was just about to start implement all the overrides for math functions (+,-,/,8, shift, etc.) When it occured to me that shouldn't all the implicit's take care of that? Do I need to over ride >= and <= when I got > < and ==?

I thought I would ask this as while there are plenty of questions relating to how to create them, there aren't many on how much is enough. Here is the code below:

public struct word_t
{
    ulong val;
    word_t(ulong val) { this.val = val; }
    public static implicit operator word_t(int a) { return new word_t((ulong)a); }
    public static implicit operator word_t(long a) { return new word_t((ulong)a); }
    public static implicit operator word_t(uint a) { return new word_t((ulong)a); }
    public static implicit operator word_t(ulong a) { return new word_t((ulong)a); }
    public static implicit operator int(word_t a) { return (int)a.val; }
    public static implicit operator long(word_t a) { return (long)a.val; }
    public static implicit operator uint(word_t a) { return (uint)a.val; }
    public static implicit operator ulong(word_t a) { return (ulong)a.val; }
    public static bool operator ==(word_t a, word_t b) { return a.val == b.val; }
    public static bool operator !=(word_t a, word_t b) { return a.val != b.val; }
    public static bool operator >(word_t a, word_t b) { return a.val > b.val; }
    public static bool operator <(word_t a, word_t b) { return a.val < b.val; }
    public override bool Equals(object obj) {
            return obj.Equals(val);
    }
    public override int GetHashCode() {
        return val.GetHashCode();
    }
    public override string toString() {
        return val.ToString();
    }
}

My gut tells me to "Trust the compiler" but my head always worries on how efficient it is.

PS I just realized I should override shifts because of the bit shifting of negative number problems, but for right now just imagine shifts just magically work like adds between uint and int.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2024-11-26 02:04:41

我推荐这篇 MSDN 文章:http://msdn。 microsoft.com/en-us/library/8edha89s(v=VS.100).aspx

它显示了您可以重载的运算符和任何捕获。您可以重载 <=>= 但它们必须成对重载,就像 ==一样! = 也是如此。

如果 + 等重载,则复杂匹配运算符 += 等可用。但是,

>=<= 是分开的。也就是说,重载 >== 不会隐式为您提供 >= 运算符。

I recommend this MSDN article: http://msdn.microsoft.com/en-us/library/8edha89s(v=VS.100).aspx

It shows the operators you can overload and any catches. You can overload <= and >= but they must be overloaded in pairs, as is true with == and != as well.

The complex match operators +=, etc are available if +, etc. is overloaded, etc.

>= and <=, however, are separate. That is, overloading > and == does not give you a >= operator implicitly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文