I know how to use references but i DO not get WHY they exist other than
to add to the language. Are they actually needed for anything? 11 1957
> I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
Since they're basically pointers in disguise, they're not really
needed, they just add some "beauty"/logical structure...
"Sven Hesse" <dr*****@users. sourceforge.net > wrote in message
news:sl******** ************@tc hibo.coffee.org ... I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
Since they're basically pointers in disguise, they're not really needed, they just add some "beauty"/logical structure...
It's not possible to overload operators without them.
-Mike
Sven Hesse wrote: I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
Since they're basically pointers in disguise, they're not really needed, they just add some "beauty"/logical structure...
References are needed as a return type for operator =, so that x = y =
z works as expected, etc. There may be a few other places where they
are truly needed.
Everywhere else, in general, they are useful because you can't override
operators on the pointers, so using references keeps things as
'objects'. Of course, in most of those cases, you could dereference,
but yeah, it would get ugly. References are needed as a return type for operator =, so that x = y = z works as expected, etc. There may be a few other places where they are truly needed.
Hmm, yeah, you're right, I haven't thought of operators, I should think
before answering next time... ^^;
(Though if the language were designed that way, you could do that with
pointer... *duck*)
but yeah, it would get ugly.
Agreed...
Sven Hesse wrote: I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
Since they're basically pointers in disguise, they're not really needed, they just add some "beauty"/logical structure...
How would you define an operator returning an lvalue without them? co************@ gmail.com wrote: I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
In some cases it gives added security over pointers. For example, the
address of a pointer can be changed which can be avoided with
reference.
On Mon, 21 Nov 2005 21:10:38 +0000 (UTC), Sven Hesse
<dr*****@users. sourceforge.net > wrote: I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
Since they're basically pointers in disguise, they're not really needed, they just add some "beauty"/logical structure...
No, they aren't pointers! This is just one more of those "urban myths"
which refuse to die. The C++ standard does not require a compiler to
allocate storage for a reference (section 9.3.2, paragraph 3). Just
because some compilers implement references as pointers doesn't mean
that *all* compilers do it that way, or if they do it sometimes, they
don't have to do that in every situation. And besides, we shouldn't
care how it is actually implemented. A compiler might reserve 1 MB of
storage for each reference for all we know, and it could still be a
"conforming implementation" . You cannot know either because sizeof()
with a reference returns the size of the object aliased, not of the
reference itself.
It also implies that compilers can take more liberty to optimize away
a reference and deal directly with the object for which it is an
alias. That's why it is not possible to take the address of a
reference, whereas a pointer will always occupy some bytes of storage
(unless it is optimized away). Finally, it is the reason why there can
be no "null references".
As others have already pointed out, there IS a real need for
references aside from what one can do with pointers.
--
Bob Hairgrove No**********@Ho me.com
On 21 Nov 2005 19:33:53 -0800, "Kaz Kylheku" <kk******@gmail .com>
wrote: Bob Hairgrove wrote: On Mon, 21 Nov 2005 21:10:38 +0000 (UTC), Sven Hesse <dr*****@users. sourceforge.net > wrote:
>> I know how to use references but i DO not get WHY they exist other than >> to add to the language. Are they actually needed for anything? > >Since they're basically pointers in disguise, they're not really >needed, they just add some "beauty"/logical structure... No, they aren't pointers! This is just one more of those "urban myths" which refuse to die.
I'm not an idiot, nor perpetrator for myths, yet I will insist that they are.
You can insist anything you like, and so can I ... but that doesn't
really make a difference about how things really are. So let's have
some more fun here... ;) The C++ standard does not require a compiler to allocate storage for a reference (section 9.3.2, paragraph 3).
The C++ standard does not require a compiler to allocate storage for other things either. Integers, pointers, etc.
It does if I take the address of an object (instance) of one of these
types or otherwise use it in some meaningful way. Just because some compilers implement references as pointers doesn't mean that *all* compilers do it that way, or if they do it sometimes, they
Name one compiler which doesn't implement a reference as a pointer, when that reference is returned from an external function, or when it's embedded in a struct or class.
This is the standard come-back: "Name one that doesn't!", and I am
tired of hearing it. I don't have time to disassemble the code
produced by the three or four compilers I regularly use in order to
see how this is done, and even if I did that for one function or
class, I couldn't be sure that the same compiler would always do it
the same way. Besides, as long as I can't use the reference any
differently than an object, I don't care -- more importantly, I don't
*have to* care. Fine with me if the assembly code shows that pointers
are used here. Why do YOU care so much, BTW?
As to reference members in a struct or class, I'd like to point out
that the standard disallows a pointer-to-member if that member has
reference type (hmm ... must be a good reason for that somewhere...). don't have to do that in every situation. And besides, we shouldn't care how it is actually implemented. A compiler might reserve 1 MB of storage for each reference for all we know, and it could still be a "conforming implementation" .
It could also reserve that storage for a pointer and be a conforming implementation . So what?
Exactly. Or it could reserve 0 bytes instead. So what?
References are /semantically/ pointers. They are functionally indistinguisha ble from pointers.
A reference can be converted to a pointer, and a pointer to a reference, without any loss of information.
Where do you get your strong guarantee that this statement is true?
The C++ standard supplies no such guarantee. As a matter of fact,
there is even a paragraph *warning* us that it is unspecified whether
or not storage is allocated for a reference. Why do you think that
sentence was deemed necessary by the standards committee? The standard
document is certainly thick enough already without meaningless text
thrown in. You cannot know either because sizeof() with a reference returns the size of the object aliased, not of the reference itself.
Who cares? You do know that a reference can store a pointer, and that you can retrieve that original pointer from it. You also know that a reference will fetch the object for you that is pointed to. These properties are enough to identify it as being, operationally, a pointer.
There is a recent thread about this; I'm not going to repeat all the arguments I made.
I'd like to see a link to it. No need to repeat anything. It also implies that compilers can take more liberty to optimize away a reference and deal directly with the object for which it is an alias.
Compilers have exactly the same liberty as with a pointer. There is an extra piece of information that is statically know about a reference, without doing any analysis: that the pointer can't be reseated. The same thing can be known about a pointer, under similar circumstances.
Moreover, a pointer can be const-qualified, so then it is statically attributed as unmodifiable.
int x; int *p const = &x;
Now, *p is an alias for x. Because p is const, we don't have to scan the scope for modifications to p, we can just assume that it always points to x. Right in the abstract syntax tree, we can replace *p occurences with x.
If the address of p is taken and escapes into some nonlocal context then we have to allocate an object for it. Big deal.
That's why it is not possible to take the address of a reference,
One reason that is not possible is that a reference isn't a type. So the resulting expression could not be mapped into the type system (other than as, say, a pointer-to-pointer).
You could easily extend the language with a special address-of operator that gives you a pointer-to-pointer to the cell that holds the reference. Of course, the compiler would then have to ensure that there is something to point to.
There is no unary && so it would fit the bill quite nicely.
int &x; int **pref = &&x; // New! extra-strength address-of!
I like it!
I think it sucks, big time.
I say make it a pointer to a non-const pointer, so you could reseat the reference:
*pref = &y; // I could use something like this from time to time!
This feature could be added to C++ without semantically conflicting with anything.
whereas a pointer will always occupy some bytes of storage (unless it is optimized away). And a reference will always occupy some bytes of storage (unless it is optimized away).
How do you know? You are generalizing from specifics and have nothing
to back it up.
Everything occupies bytes of run-time storage unless it's optimized away.
Where is the difference?
A compiler can't blindly optimize away references. Only ones that are created under the right circumstances.
Finally, it is the reason why there can be no "null references". Yes there can. Dereference a null pointer and bind a reference to that lvalue:
int &x = *(int *) 0;
Is this well-defined or not? It doesn't matter. Because the answer to that question is a double-edged sword. If it's well-defined, it means you can have null references. If it's undefined, it's worse because you can still have null references by virtue of abusing the language implementati on through undefined behavior.
Dereferencing a null pointer is always undefined behavior...the minute
it happens.
You can express the idea of creating a null reference, and C++ compilers take it without complaint.
Not only can there be null references, there can be invalid references.
int &x = *p = new int; delete p; // x is now a dangling reference
Consequently , references must be regarded with the same caution and respect as pointers. Since that's what they freaking are!
It's not so. Dangling references can be a problem, but so can dangling
pointers, integer overflow, buffer overruns, floating-point rounding
errors, etc., etc. -- in short, just about any programming error you
can name. And it's much more common to encounter dangling pointers
than dangling references, and there is a reason for this.
If have a function which takes a reference argument, inside the scope
of the body of that function I can always assume that the reference is
valid. If it isn't, then the *caller* will get undefined behavior.
It's not the responsibility of the *callee* receiving the reference to
check its validity, it's the responsibility of the caller to provide a
valid reference. This is different with pointers. Because the
*language* doesn't prohibit passing a null pointer to a function as an
argument, I have to check *inside the function* whether I can use it
or not. This is not so with references. Anyone who wastes processor
cycles writing checks like the following is just plain silly:
void func(int& ri) {
if (&ri) {
ri = 4711;
}
else {
// naughty, naughty! ...
}
}
As to passing a dangling reference, I suppose there is always that
chance. But what are you going to do about it? What are you going to
do about dangling pointers? Is there any way that is safer than a
dangling reference?
--
Bob Hairgrove No**********@Ho me.com
On 2005-11-22, Kaz Kylheku <kk******@gmail .com> wrote: Bob Hairgrove wrote: On Mon, 21 Nov 2005 21:10:38 +0000 (UTC), Sven Hesse <dr*****@users. sourceforge.net > wrote: >> I know how to use references but i DO not get WHY they >> exist other than to add to the language. Are they actually >> needed for anything? > >Since they're basically pointers in disguise, they're not >really needed, they just add some "beauty"/logical >structure...
No, they aren't pointers! This is just one more of those "urban myths" which refuse to die.
I'm not an idiot, nor perpetrator for myths, yet I will insist that they are.
You seem to be confused between an "is a" relationship and an "is
implemented in terms of" relationship.
--
Neil Cerutti This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom |
last post by:
The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.
But C++ references are so inadequate that I'm still using pointers for my
references to objects. There are 3 reasons why I find them inadequate:
1 - Can't be null.
2 - Can't be reseated.
Now I'm sure...
|
by: Dave |
last post by:
Hello all,
Please consider the code below. It is representative of a problem I am
having.
foo_t needs to contain a bar_t which is a class without a copy constructor
or operator=. It is not within my control to change bar_t. Furthermore, I
need to be able to update the contained bar_t at runtime (hence the
set_bar() method seen below).
|
by: Desmond Liu |
last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use
of references when passing parameters. I understand the reasoning behind
using references--you avoid the cost of creating a local object when you
pass an object by reference.
But why use a reference? Is there any inherent advantage of using a
reference over using a...
|
by: Dave Townsend |
last post by:
Hi,
I came across a little problem the other day, I wonder
if anyone has a more elegant solution:
I have a class which has some "pixmap" members, this is
a QT-data type which is similar to a bitmap on Windows.
A number of the objects are created and share the same set of
pixmaps, so a reference I thought would be a good thing here.
|
by: Scott Simonson |
last post by:
When in the code editor I select TOOLS | REFERENCES I get a list of
references. Mainly DLLs that are accessible. Well I assume that they are all
accessible. Besides the ones I have to use or might use, what about the
others? While scrolling through the list I saw something with AIM, AOL
Instant Messaging. Is there something in there to use in...
| |
by: S. van Beek |
last post by:
Dear reader,
For removing a reference in the VBA reference form I receive from Doug
Steele the following code:
...........
References.Remove refCurr
|
by: jeremygetsmail |
last post by:
I've got an adp (Metrix.adp) with a reference to another adp
(InteractSQL.adp). InteractSQL sits on a server, and is refered to by
all of the clients (Metrix), which sit on the client machines (There's
also a SQL Server that sits on the server, but that's besides the point
here.). Both adp files have references to ADOX. I've got to check...
|
by: Frederick Gotham |
last post by:
When I was a beginner in C++, I struggled with the idea of references.
Having learned how to use pointers first, I was hesitant to accept that
references just "do their job" and that's it.
Just recently, a poster posted looking for an explanation of references.
I'll give my own understanding if it's worth anything.
First of all, the C++...
|
by: igor.kulkin |
last post by:
References is a relatively basic feature of C++ language.
It might be a good thing to think of references as aliases to the
variables.
However it's good to think of references this way when you deal with
references which are local variables.
But references can also be function arguments (in fact they are more
useful this way) in which case...
|
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. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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.
| |
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...
| |