Re: why creating pointer to reference member is illegal?
In article Daniel T. wrote:
blargg.ei3@gishpuppy.com (blargg) wrote:
[...]
typedef int T; // why doesn't this work if changed to int&?
struct X { T i; };
void example( X& x )
{
T X::*mptr = &X::i; // get pointer to member
x.*mptr = 123; // now dereference it on an object
}
Why can't we do the same thing when i is of type int&?
For the same reason this doesn't work.
typedef int& T;
void example( T x )
{
T* p = &x; // get pointer to member
}
That doesn't work because it's senseless: the thing that x refers to
cannot change, so there's no reason to want a pointer to the reference;
you might as well just get a pointer to the object itself. The same
applies to getting a pointer to a reference member of an object.
void example( int& ref )
{
int&* p = &ref; // illegal, and behavior indistinguishable from
int* p = &ref; // taking a pointer to it
}
But he wanted to get a pointer to member of NO PARTICULAR object, which
IS a useful operation. It's a way to turn something compile-time, which
member one is operating on, into something run-time, so that a single
function can operate on different members, selected at run-time. Thus it
makes just as much sense to want to have a pointer to reference member
as a pointer to any other member type.
struct X
{
int& a;
int& b;
};
void use_member( int& X::*m, X& x )
{
x.*m = 1234;
}
How would you code the above without changing the types of X::a and
X::b, without using a pointer to member, and without coding multiple
versions of use_member? You'd have to resort to passing accessor
functions:
int& get_a( X& x ) { return x.a; }
int& get_b( X& x ) { return x.b; }
void use_member( int& (*m)( X& ), X& x )
{
m( x ) = 1234;
}
That's an ugly workaround. The pointer-to-member situation is
fundamentally different, so I just don't understand why people keep
bringing up the issue with trying to have a pointer to reference; this
is different!
And neither works for the exact reason I said earlier, you can only take
pointers to objects and functions... A reference is neither an object or
a function.
This shows how it fails, but not why. The why would be like the
explanation as to why one can't get a pointer to a reference (as I
showed above in example code, that it doesn't allow anything new).
If you want to get deeper into the question, the next logical thing to
ask would be, why have a member reference in the first place? References
were not designed to be member variables.
I assume you mean a member that's a reference, as opposed to a reference
to member. That's a bit better answer, but plenty of people use
references as members in order to capture the fact that the referent
never changes during the lifetime of the object. This might be a good
argument to avoid using references except as function arguments, since
they have arbitrary and unnecessary limitations like this.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]