How to implement pre and post-increment / decrement operator in my class?
I want to overload ++
开发者_如何学运维operator to use pre-increment and post-increment using operator overloading in my c# class. But only post-increment is working. How to make both function works in my class?
Suppose I made a class ABC like -
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class ABC
{
public int a,b;
public ABC(int x, int y)
{
a = x;
b = y;
}
public static ABC operator ++(ABC x)
{
x.a++;
x.b++;
return x;
}
}
class Program
{
static void Main(string[] args)
{
ABC a = new ABC(5, 6);
ABC b, c;
b = a++;
Console.WriteLine("After post increment values are {0} and {1} and values of b are {2} and {3}", a.a, a.b, b.a, b.b);// expected output a.a = 6, a.b = 7, b.a = 5, b.b = 6 but not get that
c = ++a;
Console.WriteLine("After pre increment values are {0} and {1} and values of c are {2} and {3}", a.a, a.b, c.a, c.b); // expected output a.a = 7, a.b = 7, c.a = 7, c.b = 8 works fine
Console.Read();
}
}
}
Your sample fail to implement this unary operator correctly, as specified in the C# specification in 17.9.1 Unary operators :
Unlike in C++, this method need not, and, in fact, should not, modify the value of its operand directly.
Here is your sample with some micro unit tests :
using System;
class ABC
{
public int a,b;
public ABC(int x, int y)
{
a = x;
b = y;
}
public static ABC operator ++(ABC x)
{
x.a++;
x.b++;
return x;
}
}
class Program
{
static void Main()
{
var a = new ABC(5, 6);
if ((a.a != 5) || (a.b != 6)) Console.WriteLine(".ctor failed");
var post = a++;
if ((a.a != 6) || (a.b != 7)) Console.WriteLine("post incrementation failed");
if ((post.a != 5) || (post.b != 6)) Console.WriteLine("post incrementation result failed");
var pre = ++a;
if ((a.a != 7) || (a.b != 8)) Console.WriteLine("pre incrementation failed");
if ((pre.a != 7) || (pre.b != 8)) Console.WriteLine("pre incrementation result failed");
Console.Read();
}
}
Your code fail is the post incrementation result and it is due to the fact that you alter the instance of ABC passed as parameter instead of returning a new instance. Corrected code :
class ABC
{
public int a,b;
public ABC(int x, int y)
{
a = x;
b = y;
}
public static ABC operator ++(ABC x)
{
return new ABC(x.a + 1, x.b + 1);
}
}
Here is the sample from the spec, what differ in your case ?
public class IntVector
{
public int Length { … } // read-only property
public int this[int index] { … } // read-write indexer
public IntVector(int vectorLength) { … }
public static IntVector operator++(IntVector iv) {
IntVector temp = new IntVector(iv.Length);
for (int i = 0; i < iv.Length; ++i)
temp[i] = iv[i] + 1;
return temp;
}
}
class Test
{
static void Main() {
IntVector iv1 = new IntVector(4); // vector of 4x0
IntVector iv2;
iv2 = iv1++; // iv2 contains 4x0, iv1 contains 4x1
iv2 = ++iv1; // iv2 contains 4x2, iv1 contains 4x2
}
}
精彩评论