Re: const correct member variable access thru function?
The proper way to do it (IMO) would be to use a proxy class that would
know whether it's on the right or on the left side of the assignment op.
It's not as simple as introducing two overloaded functions, like here:
Thanks, I cant believe i did not think of overloading..
because in this case if 'A' is non-const, then the non-const version
will be called if you use it in an expression (even though you're not
trying to assign to it):
That is interseting. So if I wanted to log read and write access to the
variable then overloading would not work.. hmm, I had a go at making a
proxy object emulating the variable, instead of having functions
emulating the variable. The same problem seems to creep up though. That
is, if Z is a non const object and I do a 'int tmp = z.x;' then the non
const assignment operator is called even though this is a "read only"
operation. Anyone got any tips or links to related threads?
Example code (take 2):
template <class T>
struct Proxy {
public:
Proxy(T &x): x_ref_(x) {} //init ref
//assignment operator..
Proxy<T>& operator = (const T& rhs) {x_ref_ = rhs; /*cout<<"non-const
lhs";*/ return *this;}
//conversion operator..
operator T& () { /*cout<<"non-const rhs";*/ return x_ref_; }
operator const T& () const { /*cout<<"const rhs";*/ return x_ref_; }
private:
T &x_ref_;
};
struct A{
public:
int x;
};
struct Z {
private:
int x_;
public:
Proxy<int> x;
Z(): x_(0), x(x_) {;}
};
void main() {
int tmp;
A a = A();
a.x = 1;
tmp = a.x;
const A a_const = A();
tmp = a_const.x;
//---//
Z z = Z();
z.x = 1;
//tmp = z.x; // error C2593: 'operator =' is ambiguous. // ehh!?
{
Z z = Z();
z.x = 1;
int tmp = z.x; // calls non-const operator=() // calling non const
even though this is read only !?
}
{
const Z z_const = Z();
int tmp = z_const.x; // calls const operator =
}
}
Regards Patrik
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]