473,378 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

a pointer to a reference

C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

Sep 15 '06 #1
11 4162

asdf wrote:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
What would be its purpose?

Sep 15 '06 #2
asdf wrote:

C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
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.

Best regards,

Tom

Sep 15 '06 #3
Thomas Tutone <Th***********@yahoo.comwrote:
asdf wrote:
>C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

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.
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.
Now a reference is simply an alias
for another variable, so p_ref==p,
This I agree with.
but that still contradicts your
premise.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 15 '06 #4
Thomas Tutone <Th***********@yahoo.comwrote:
asdf wrote:
>C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

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.
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
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 15 '06 #5

Marcus Kwok wrote:
Thomas Tutone <Th***********@yahoo.comwrote:
asdf wrote:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
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.

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
}
Good point. I hadn't thought of it that way.

In any case, I guess the reason is that how a reference is implemented
is, by definition, an implementation detail. In some instances, a
reference might be implemented under the hood as as a const pointer, in
other instances it may be a true alias with no separate storage. In
both cases, but particularly the latter, a pointer to the alias, as
opposed to the aliased object, wouldn't make much sense.

Best regards,

Tom

Sep 15 '06 #6
asdf wrote:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
Of course it does. A pointer to a reference is a pointer to the target
object.

int i = 3;
int &ri = i;
int *pi = &ri; // pointer to reference

But, of course, this isn't being done with a special
"pointer-to-reference" type, but a regular pointer.

This is consistent with the "invisible" semantics of a reference.

There would be no utility in pointing to a reference. Dereferencing
that pointer would cause the resulting lvalue to "slide" to the target
object.

Since a reference cannot be reseated, you would not be gaining any
additional level of indirection by doing this.

What's worse, you could create new modes of failure. The reference can
potentially have a shorter lifetime than the object it points to,
meaning that the reference could be destroyed, thus invalidating the
pointer-to-reference even though the target object still exists.

Lastly, there is no syntax for obtaining the address of a reference, so
you'd have no way to initialize such a pointer. The address of a
reference yields the address of the referent object. So
pointers-to-reference would have to be accompanied by special syntax.

Sep 15 '06 #7
asdf wrote:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
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.
Sep 15 '06 #8
"asdf" <li*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
Because a reference really doesn't exist as an object per se.

In reality, most compilers probably implement a reference as a pointer and
treat it differently, but they don't have to. It could be some entry in a
table, or anything else a compiler designer wanted to implement, AFAIK.

And there is the rule that a reference can't be reseated.

As such, a pointer to a reference really gives you nothing. What would it
actually point to? The reference may no actually be stored in memory
anywhere (although it most likely is). What would you do with this pointer
to a reference? You can't change the reference itself (they're not allowed
to be reseated).

References don't follow the same types of rules as pointers, such as you
can't store them in an array, etc.. (AFAIK, I may be wrong on this, but I
think it's right).

If you need to get a pointer to a reference, then most likely you don't want
a referance in the first place but a pure pointer. Pointers to pointers are
allowed.
Sep 15 '06 #9
Marcus Kwok wrote:
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.
Of course a reference exists on its own.
>
Now a reference is simply an alias
for another variable, so p_ref==p,

This I agree with.
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. 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.

Sep 15 '06 #10
Gianni Mariani wrote:
asdf wrote:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

References are special beasts in that they may or may not take storage.
.... an attribute which they share with constants and lexical variables.
From an implementation perspective, it's impossible to point to
something that takes no storage.
That's completely irrelevant, since you can point to constants and
variables. When you take their address, the language implementation
ensures that something exists that allows the pointer to be
instantiated. I.e. it has to defeat certain optimizations from taking
place.

The same could be arranged for a pointer-to-reference mechanism.

The real reason why there isn't one is that it would be close to
useless, and supporting it would require additional syntax and
semantics to be present in references, which would basically reduce
them to having some of the functionality of pointers which they
deliberately do not have.
The best you can do is create a struct that contains a reference (and
hence storage) and point to it.
Wait a minute, didn't you say that references may not take storage? Doh!

Sep 15 '06 #11
Kaz Kylheku <kk******@gmail.comwrote:
Marcus Kwok wrote:
>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.

Of course a reference exists on its own.
I contend that a reference can't exist without something to refer to.
Now a reference is simply an alias
for another variable, so p_ref==p,

This I agree with.

That is only an optimization.
I was just confirming the actions of the (snipped) code.
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.
Right, but AFAIK the Standard does not.
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.
OK, but in each case, you have the reference and the thing it refers to.
The objects can exist without the references, but the references cannot
exist without the objects.
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.
OK, but I would consider this an implementation detail.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.