And why and where one should use one vs. the other?
Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...
TIA
Jul 22 '05
14 2541
"bo" <bo@cephus.co m> skrev i en meddelelse
news:9q******** *************** *********@4ax.c om... And why and where one should use one vs. the other?
Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over pointers in some cases...
TIA
Have you read the faq pointed to by others? I believe it is quite good. No
specific implementation is required in the standard. That said, you can be
quite sure that a reference in the general case will be represented by a
pointervalue. In special cases, the value of the reference will be known and
no pointer will be stored.
/Peter
the main usefulness in my mind is maintainability ; once you create a
reference variable you *know* it is always going to point to the same
thing, and that it is going to be non-null. But if you are reading code
with pointers in it you may not be sure that the author isn't going to
go changing the meaning later down in the code, and if he does you may
not be able to tell immediately whether it was intended or a bug.
Internally however, compilers I have seen have typically implemented
reference variables the same way they did pointers in terms of low-level
behavior.
Personally I spend a *lot* more time maintaining and reusing than I do
cutting new code, so any help I can get in terms of cues as to the
intended purpose and/or limitations of things are really helpful.
David
bo wrote: And why and where one should use one vs. the other?
Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over pointers in some cases...
TIA
Thanks to all who replied and the pointer (reference?) to the FAQ on
the subject! I am enlightened.... .
Regards,
Bo
On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the
following: bo posted:
And why and where one should use one vs. the other?
.... References are nicer to work with, in that they behave exactly like real variables.
a) They can be reseated. One minute they can point East, the next South West.
This is probably just style, but this is why Pointers Are
Bad(TM). ;-)
b) They can be incremented/decremented. If you have a pointer to a character, you simply increment it and it points to the next character in the string.
I wouldn't even do this. If I really had to reference
string chars, I'd probably use *(ptr + i) and so on (i.e.,
not change the pointer itself if I didn't have to.) but
I guess we've already covered that, haven't we? (i.e. then
use a reference.) :-)
So...
Use references. If you can't, eg. you need to increment it or seseat it, then *resort* to pointers.
Yeah - I cut my teeth on C with K&R at my elbow, and a tutor
who was about as smart as me - I wrote in a lot of printfs. ;-)
and have done a little thinking about pointers vs. references,
and I can't think of a single thing I'd want to do in real C++
that would need a pointer at all. I'm not porting any legacy
code - I intend to lift all new snippets to paste. ;-)
(I can pass for a programmer, but mostly what I do is copy
and paste other people's code. :-)
Cheers!
Rich
In message <btT2d.1520$Co1 .271@trnddc02>, Rich Grise <nu**@example.n et>
writes On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the following:
[...] b) They can be incremented/decremented. If you have a pointer to a character, you simply increment it and it points to the next character in the string.
I wouldn't even do this. If I really had to reference string chars, I'd probably use *(ptr + i) and so on (i.e., not change the pointer itself if I didn't have to.)
Pointers Are Iterators, so there's no harm in using the natural iterator
operations with them.
but I guess we've already covered that, haven't we? (i.e. then use a reference.) :-)
So...
Use references. If you can't, eg. you need to increment it or seseat it, then *resort* to pointers. Yeah - I cut my teeth on C with K&R at my elbow, and a tutor who was about as smart as me - I wrote in a lot of printfs. ;-) and have done a little thinking about pointers vs. references, and I can't think of a single thing I'd want to do in real C++ that would need a pointer at all.
See above.
I'm not porting any legacy code - I intend to lift all new snippets to paste. ;-) (I can pass for a programmer, but mostly what I do is copy and paste other people's code. :-)
--
Richard Herring This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ROSY |
last post by:
1. How would you use the functions memcpy(), memset(), memmove()?
|
by: Ching-Lung |
last post by:
Hi all,
I try to create a tool to check the delta (diff) of 2
binaries and create the delta binary. I use binary
formatter (serialization) to create the delta binary. It
works fine but the delta binary is pretty huge in size. I
have 1 byte file and 2 bytes file, the delta should be 1
byte but somehow it turns out to be 249 bytes using binary
formatter. I guess serialization has some other things
added to the delta file.
|
by: Robin Tucker |
last post by:
Although I've been working on this project for 8 months now, I'm still not
sure of the difference between ByVal and ByRef. As most objects in VB are
reference types, passing ByVal I've discovered allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?
I'm coming from a C++ background...
|
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: Rob Hoelz |
last post by:
I've been compiling a list of the differences between C and C++, and
I'd like to see how thorough I've been. Could any of you go over this
list and see if I've missed anything?
Here's the list:
Mid-scope declarations
Default function parameters
new/delete operators
Better implementation of const
| |
by: Jo |
last post by:
Hi,
How can i differentiate between static and dynamic allocated objects?
For example:
void SomeFunction1() {
CObject *objectp = new CObject;
CObject object;
|
by: panks |
last post by:
What is diff between
Class c=new Class(); //Create object
and
Class c;
c=new Class(); //create reference variable
|
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:
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: 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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |