Reputation: 28818
Take this class and operator overload:
public class Test
{
public static Test operator +(Test test)
{
return test;
}
}
Ignoring the implementation simply returning "test" for now, in what circumstance would this operator overload be called? It is valid and compiles, but I cannot work out what it's for!
It only takes one parameter, so what it is adding?
Upvotes: 0
Views: 38
Reputation: 23732
so what it is adding?
I think it does not add anything but determines a polarity in a sense. I guess it would be comparable to a sign saying negative and positive like in
int i = +5;
This compiles just fine:
Test t1 = new Test();
Test t2 = +t1;
but this doesn't:
Test t4 = -t1;
It compiles after the addition of
public static Test operator -(Test test) { return test; }
Here is what MSDN has to say to the unary + operator
The result of the unary plus operator (+) is the value of its operand
The unary negation operator (–) produces the negative of its operand
Upvotes: 3