What you mean is very conterintuitive. But this can not be achived that way.
What you want is ordinary done with the help of proxy objects, like it is done in std::vector<bool>
specialization. When you using it like v[i] = true;
, v[i]
returns proxy object, that have overloaded assignment operator, which rises ith
bit in internal bit-string.
Example:
struct A
{
struct proxy
{
proxy(int * x)
: x_(x)
{
}
proxy & operator = (int v)
{
*x_ = v + 2;
return *this;
}
operator int() const
{
return *x_;
}
private:
int * x_;
};
proxy x_add_2()
{
return proxy(&x_);
}
int x() const
{
return x_;
}
private:
int x_;
};
int main(int argc, char* argv[])
{
A a;
a.x_add_2() = 2;
std::cout << a.x() << std::endl;
return 0;
}