I am working on lazy object composer
class Bitset
{
private List<bool> _data;
public Bitset(List<bool> vector)
{
_data = vector;
}
public virtual bool GetElement (int i)
{
return _data[i];
}
public Bitset(){}
}
class BitsetComposer:Bitset
{
readonly private Bitset _a,_b;
private Func<bool,bool,bool> _composer;
public BitsetComposer(Bitset a,Bitset b, Func<bool,bool,bool> composer)
{
this._a=a;
this._b=b;
this._composer=composer;
}
public override bool GetElement (int i)
{
return _composer(_a.GetElement(i),_b.GetElement(i));
}
public static BitsetComposer operator & (BitsetComposer a, BitsetComposer b)
{
return new BitsetComposer(a,b,BitsetComposer.And);
}
public static bool And(bool a,bool b){return a&b;}
}
This is not fine, because, it would be desired that & can also take base class
//replaced with
//public static BitsetComposer operator & (BitsetComposer a, BitsetComposer b)
public static BitsetComposer operator & (Bitset a, Bitset b)
{
return new BitsetComposer(a,b,BitsetComposer.And);
}
But I got error:
Error CS0563: One of the parameters of a binary operator must be the containing type
If both operands are expected to be of base class type, you only need to move the
operatorcode to the base class.From 7.2.2 Operator overloading: