Re: why creating pointer to reference member is illegal?
Pavel Minaev wrote:
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 why you can't have a pointer to a non-member
reference - because it's not an object, meaning that there may be
nothing to point to.
[...]
For an example of when a compiler could possibly have a member
reference that doesn't use storage, consider this:
class foo {
foo& self;
foo() : self(*this) {}
};
It seems quite possible for a smart code analyzer to figure out
that self is always bound to *this, and that all references to self
are going to happen where *this is available anyway (since self is
private), thus allowing for direct substitution, and making the
storage redundant. I do not know of any compiler that actually does
that sort of thing today, but I don't see why it would be invalid.
OK, that makes more sense. It's sort of like the "register" keyword,
where the object may never even be put in memory. The difference is that
because the reference may be used from other compilation units, the
decision of whether to put it in memory must be made in the absense of
this information, so the language takes the approach that it never has
to be stored in memory. The register analogy would be that declaring
something as a register prevented taking its address at all (which isn't
the case of course, because the compiler knows every use of the object
and can thus decide whether it needs to be in memory).
The stock "you can't get a pointer to reference member because a
reference isn't an object" that I kept getting was not a very helpful.
In case anyone would like to improve future responses to people, here
are the two that are much more helpful:
To the question of why one can't get a pointer to reference, the best
answer is "It wouldn't serve any purpose. A given reference always
refers to the same object; it cannot be changed once the reference is
initialized. Thus, a pointer to a reference would behave the same as a
pointer to the object the reference refers to."
To the question of why one can't get a pointer to reference MEMBER of a
class, the best answer is "References aren't required to exist in
memory, so there might not even be a PHYSICAL member to get a pointer
to. A given reference member might be initialized in all the
constructors in a common way that allows the compiler to determine the
object it refers to without needing to store anything in memory. When
using that particular reference, the compiler can find how to determine
the object it refers to, via a means different than a simple stored
pointer. A pointer-to-member is aimed only at things in memory, so it
can't handle things that must be calculated at run-time. True, a pointer
to member implementation could instead be represented as an accessor
function, which could then handle any member type, but this would bloat
the mechanism for a very minor benefit."
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]