473,795 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4243

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*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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_referen ce(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

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

Similar topics

110
9967
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 must be an object instead of
9
3004
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 tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
18
3541
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
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
2684
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
4477
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
2402
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
33
5083
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 actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
2
35625
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 implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
41
3689
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 pointer and reference are and how they relate to each other.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.