运算符重载导致堆栈溢出
几天前我开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,一方面,运算符重载不会破坏您的代码。您收到
StackOverflowException
是因为您的text
属性的 getter 试图返回自身。您应该为您的属性使用支持字段:
.NET 在幕后所做的是将您的属性转换为访问器和修改器——两个独立的方法。在您的原始示例中,您的代码将执行以下操作:
而更正后的版本正确使用支持字段:
Well, for one, the operator overloading isn't breaking your code. You get a
StackOverflowException
because yourtext
property's getter is trying to return itself.You should use a backing field for your property:
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:
Whereas the corrected version uses the backing field properly:
类中的 get 代码块有问题,这就是导致 StackOverFlow 异常的原因。
在这里,当您说
return text;
时,它将去调用属性text
本身的 get 块,这会导致堆栈溢出。将您的属性文本包裹在私有 _txt 字符串字段中,它应该可以正常工作。你可以把它做成这样的..
The get code-block in your class has the problem and that is what is causing the StackOverFlow exception.
Here when you say
return text;
it will go and call the get block of the propertytext
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..
问题是 text 属性返回自身
您应该有一个受保护或私有变量来存储结果:
The problem is that the text proprty returns itself
You should have a protected or private variab le to store the resut: