运算符重载导致堆栈溢出

发布于 2024-10-19 14:23:33 字数 1692 浏览 5 评论 0原文

几天前我开始使用 C# 进行编程。

现在,在处理运算符重载时出现了一个令人困惑的错误。

以下代码在运行时产生 StackOverflowException

using System;

namespace OperatorOverloading
{
    public class Operators
    {
        // Properties
        public string text
        {
            get
            {
                return text;
            }

            set
            {
                if(value != null)
                    text = value;
                else
                    text = "";
            }
        }

        // Constructors
        public Operators() : this("")
        {
        }

        public Operators(string text)
        {
            // Use "set" property.
            this.text = text;
        }

        // Methods
        public override string ToString()
        {
            return text;
        }

        // Operator Overloading
        public static string operator +(Operators lhs, Operators rhs)
        {
            // Uses properties of the passed arguments.
            return lhs.text + rhs.text;
        }

        public static void Main(string[] args)
        {
            Operators o1 = new Operators();
            Operators o2 = new Operators("a");
            Operators o3 = new Operators("b");

            Console.WriteLine("o1: " + o1);
            Console.WriteLine("o2: " + o2);
            Console.WriteLine("o3: " + o3);

            Console.WriteLine();

            Console.WriteLine("o1 + o2: " + (o1 + o2));
            Console.WriteLine("o2 + o3: " + (o2 + o3));
        }
    }
}

在阅读了 Dirk Louis 和 Shinja Strasser 所著的《Microsoft Visual C# 2008》一书中有关运算符重载的章节后,我尝试编写一个自己的示例。

也许有人知道出了什么问题。

谢谢。

I started out programming with C# a few days ago.

Now an confusing error arised when playing around with operator overloading.

The following code produces a StackOverflowException when running:

using System;

namespace OperatorOverloading
{
    public class Operators
    {
        // Properties
        public string text
        {
            get
            {
                return text;
            }

            set
            {
                if(value != null)
                    text = value;
                else
                    text = "";
            }
        }

        // Constructors
        public Operators() : this("")
        {
        }

        public Operators(string text)
        {
            // Use "set" property.
            this.text = text;
        }

        // Methods
        public override string ToString()
        {
            return text;
        }

        // Operator Overloading
        public static string operator +(Operators lhs, Operators rhs)
        {
            // Uses properties of the passed arguments.
            return lhs.text + rhs.text;
        }

        public static void Main(string[] args)
        {
            Operators o1 = new Operators();
            Operators o2 = new Operators("a");
            Operators o3 = new Operators("b");

            Console.WriteLine("o1: " + o1);
            Console.WriteLine("o2: " + o2);
            Console.WriteLine("o3: " + o3);

            Console.WriteLine();

            Console.WriteLine("o1 + o2: " + (o1 + o2));
            Console.WriteLine("o2 + o3: " + (o2 + o3));
        }
    }
}

I tried to write an own example after reading the chapter about operater overloading from the book "Microsoft Visual C# 2008" from Dirk Louis and Shinja Strasser.

Maybe someone has a clue what's going wrong.

Thanks.

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

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

发布评论

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

评论(3

红颜悴 2024-10-26 14:23:33

嗯,一方面,运算符重载不会破坏您的代码。您收到 StackOverflowException 是因为您的 text 属性的 getter 试图返回自身。

您应该为您的属性使用支持字段:

private string _text;

public string Text
{
    get { return _text; }
    set
    {
        if (value != null)
            _text = value;
        else
            _text = string.Empty;
    }
}

.NET 在幕后所做的是将您的属性转换为访问器和修改器——两个独立的方法。在您的原始示例中,您的代码将执行以下操作:

private string text;

public string get_text()
{
    return get_text(); // <-- StackOverflowException
}

public void set_text(string value)
{
    this.text = value;
}

而更正后的版本正确使用支持字段:

private string text;

public string get_text()
{
    return this.text; // Happy :)
}

public void set_text(string value)
{
    this.text = value;
}

Well, for one, the operator overloading isn't breaking your code. You get a StackOverflowException because your text property's getter is trying to return itself.

You should use a backing field for your property:

private string _text;

public string Text
{
    get { return _text; }
    set
    {
        if (value != null)
            _text = value;
        else
            _text = string.Empty;
    }
}

What .NET does under the covers is convert your property into an accessor and a mutator -- two separate methods. In your original example, your code would be doing the following:

private string text;

public string get_text()
{
    return get_text(); // <-- StackOverflowException
}

public void set_text(string value)
{
    this.text = value;
}

Whereas the corrected version uses the backing field properly:

private string text;

public string get_text()
{
    return this.text; // Happy :)
}

public void set_text(string value)
{
    this.text = value;
}
笨笨の傻瓜 2024-10-26 14:23:33

类中的 get 代码块有问题,这就是导致 StackOverFlow 异常的原因。

    public string text
    {
        get
        {
            return text;
        }
    }

在这里,当您说 return text; 时,它将去调用属性 text 本身的 get 块,这会导致堆栈溢出。将您的属性文本包裹在私有 _txt 字符串字段中,它应该可以正常工作。

你可以把它做成这样的..

private string _txt;
public string text
{
    get
    {
        return _txt;
    }

    set
    {
        _txt = string.IsNullOrEmpty(value) ? string.Empty : value;
    }
}

The get code-block in your class has the problem and that is what is causing the StackOverFlow exception.

    public string text
    {
        get
        {
            return text;
        }
    }

Here when you say return text; it will go and call the get block of the property text itself which is causing the stack-overflow. wrap your property text around a private _txt string field and it should work properly.

You can make it something like this..

private string _txt;
public string text
{
    get
    {
        return _txt;
    }

    set
    {
        _txt = string.IsNullOrEmpty(value) ? string.Empty : value;
    }
}
携君以终年 2024-10-26 14:23:33

问题是 text 属性返回自身
您应该有一个受保护或私有变量来存储结果:

// Properties
    private string _text
    public string text
    {
        get
        {
            return _text;
        }

        set
        {
            if(value != null)
                _text = value;
            else
                _text = "";
        }
    }

The problem is that the text proprty returns itself
You should have a protected or private variab le to store the resut:

// Properties
    private string _text
    public string text
    {
        get
        {
            return _text;
        }

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