C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?
Sep 15 '06
11 4244
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!
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_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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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???????
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
by: al.cpwn |
last post by:
I get that these two are different
int* get()
{
static int m;
return &m;
}
int& get()
{
| |
by: Kuku |
last post by:
What is the difference between a reference and a pointer?
|
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);
|
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
|
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...
|
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.
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |