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.
Operator overloading facilitates the specification of user-defined implementation for operations wherein one or both operands are of user-defined class or structure type. This helps user-defined types to behave much like the fundamental primitive data types.
Operator Overloading in C++ In C++, we can make operators work for user-defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
It allows you to provide an intuitive interface to users of your class, plus makes it possible for templates to work equally well with classes and built-in/intrinsic types. Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes).
1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With