2013-08-28 52 views
-1

公共類EncryptVal { 私人EncryptVal(T值) { 集(值); }運算符重載在C# 「+」 和 「+ +」

// Encrypt 
public bool Set(T value) 
{} 

// Decrypt & return (T)value; 
public T Get() 
    {} 

public static implicit operator EncryptVal<T>(T value) { 
    //shuffle bits 
    return new EncryptVal<T>(value); 
} 

public static implicit operator T(EncryptVal<T> value) { 
    //unshuffle bits 
    return value.Get(); ///// error ... 
} 
} 

我不能重載+運算& ++運算符。我怎樣才能做到這一點?

用法:

EncryptVal<int> e = 42; 
EncryptVal<int> f = 1; 
Console.WriteLine("decrypt int count \t= {0:d}", f.Get()); // ok 
Console.WriteLine("decrypt int count \t= {0:d}", f); // error 
Console.WriteLine("decrypt int count \t= {0:d}", e); //how? 
+6

您顯示的代碼與問題沒有多大關係。我既沒有看到+運算符的重載,也沒有看到它的用法。 –

+0

我怎麼做這個+運算符&++操作符。 T是int,float,double。和你會得到價值只有變量沒有使用()得到? – user2725053

回答

0

超載+++只需添加你想要的+/++運營商...

public static EncryptVal<T> operator ++(EncryptVal<T> value) 
{ 
    throw new NotImplementedException(); // your implementation here 
} 
public static EncryptVal<T> operator +(EncryptVal<T> lhs, string rhs) 
{ 
    throw new NotImplementedException(); // your implementation here 
} 
public static EncryptVal<T> operator +(EncryptVal<T> lhs, EncryptVal<T> rhs) 
{ 
    throw new NotImplementedException(); // your implementation here 
} 
public static EncryptVal<T> operator +(EncryptVal<T> lhs, int rhs) 
{ 
    throw new NotImplementedException(); // your implementation here 
} 
public static EncryptVal<T> operator +(bool lhs, EncryptVal<T> rhs) 
{ 
    throw new NotImplementedException(); // your implementation here 
} 

目前尚不清楚這涉及到問題的其餘部分,但是。 特別是,Console.WriteLine可能要public override ToString()更重要。