A
asdf
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
reference, why?
asdf said:C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
Thomas Tutone said:You're mistaken:
int main()
{
int i;
int& ref_i = i; // reference to an int
int* p = &i; // pointer to an int
int* p_ref = &ref_i; // pointer to a reference to an int.
}
p_ref is a pointer to a reference.
Now a reference is simply an alias
for another variable, so p_ref==p,
Thomas Tutone said:You're mistaken:
int main()
{
int i;
int& ref_i = i; // reference to an int
int* p = &i; // pointer to an int
int* p_ref = &ref_i; // pointer to a reference to an int.
}
p_ref is a pointer to a reference. Now a reference is simply an alias
for another variable, so p_ref==p, but that still contradicts your
premise.
Marcus said:As another followup, taken literally it is illegal:
int main()
{
int i = 42;
int& r = i;
int&* p = &r; // illegal: pointer to reference is not allowed
}
asdf said:C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
asdf said:C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
asdf said:C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
Marcus said:I tend to disagree. Once a reference has been seated, any use of it
really refers to the referenced object, so (p_ref = &ref_i) really means
(p_ref = &i), so it is a pointer to the referenced object, but not
really a pointer to the actual reference. Though, this may be splitting
hairs, and I can see how your interpretation can be seen as correct too,
since a reference doesn't really exist on its own.
This I agree with.
Gianni said:References are special beasts in that they may or may not take storage.
From an implementation perspective, it's impossible to point to
something that takes no storage.
The best you can do is create a struct that contains a reference (and
hence storage) and point to it.
Kaz Kylheku said:Of course a reference exists on its own.
That is only an optimization.
In the general case, references are real
run-time entities. They just aren't integrated into the type system as
first-class objects, that's all.
Yes.
Many situations require references to
occupy memory locations.
In these examples, references must correspond to something in the
run-time:
extern int &return_reference(void);
If a reference isn't real, how does the above function return
something? If it was inlined, then the reference could disappear, but
how can that happen under an external call? The function can return the
location of some arbitrary object. That location can be captured by the
caller, who can then modify that object. The function can choose a
different object each time you call it; and the object can even be
dynamically allocated.
How about:
struct s {
int &r;
};
If a reference isn't real, shouldn't sizeof(s) be 1? How will the
program retrieve the member r from an arbitrary struct s? There could
be millions of dynamically allocated instances of struct s, all with
different references r.
The compiler cannot account for these with a compile-time alias trick.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.