You can't store a reference to an integer like that directly, but you can store a reference to the GlorifiedInt object containing it. In your case, what I'd probably do is make the BitAccessor class nested inside GlorifiedInt (so that it gets access to private fields), and then pass it a reference to this when it's created, which it can then use to access the m_val field. Here's an example which does what you're looking for:
class Program
{
static void Main(string[] args)
{
var g = new GlorifiedInt(7);
g.Bits[0] = false;
Console.WriteLine(g.Value); // prints "6"
}
}
class GlorifiedInt
{
private int m_val;
public GlorifiedInt(int value)
{
m_val = value;
}
public int Value
{
get { return m_val; }
}
public BitAccessor Bits
{
get { return new BitAccessor(this); }
}
public class BitAccessor
{
private GlorifiedInt gi;
public BitAccessor(GlorifiedInt glorified)
{
gi = glorified;
}
public bool this[int index]
{
get
{
if (index < 0 || index > 31)
throw new IndexOutOfRangeException("BitAcessor");
return (1 & (gi.m_val >> index)) == 1;
}
set
{
if (index < 0 || index > 31)
throw new IndexOutOfRangeException("BitAcessor");
if (value)
gi.m_val |= 1 << index;
else
gi.m_val &= ~(1 << index);
}
}
}
}