-1

公共类 EncryptVal { private EncryptVal(T value) { Set( value ); }

    // 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?
4

1 回答 1

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()

于 2013-08-28T11:17:43.480 回答