473,804 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++: pointers Vs references

Hi,

Can someone provide a short and concise statement(s) on the difference
between pointers and references. A graphical representation (via links?) of
both would be much appreciated as well.
WD

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
Jul 19 '05 #1
15 14041
Web Developer <no****@hotmail .com> wrote in message
news:3f******** **@news.iprimus .com.au...
Hi,

Can someone provide a short and concise statement(s) on the difference
between pointers and references.
A pointer is an object containing the address in memory of another object; a
reference is an alias for another object.
A pointer can be null; a reference cannot.
A pointer can be re-seated (i.e., pointed at a different object); a
reference cannot.
A reference must be initialized (i.e., its refer-ee specified) when it is
created; a pointer can be uninitialized.
A graphical representation (via links?) of
both would be much appreciated as well.


I'm not sure what one would look like. Anyway, I am unable to provide one.

DW

Jul 19 '05 #2

"David White" <no@email.provi ded> wrote in message
news:ta******** ********@nasal. pacific.net.au. ..
Web Developer <no****@hotmail .com> wrote in message
news:3f******** **@news.iprimus .com.au...
Hi,

Can someone provide a short and concise statement(s) on the difference
between pointers and references.
A pointer is an object containing the address in memory of another object;

a reference is an alias for another object.
A pointer can be null; a reference cannot.
A pointer can be re-seated (i.e., pointed at a different object); a
reference cannot.
A reference must be initialized (i.e., its refer-ee specified) when it is
created; a pointer can be uninitialized.


Also a pointer is syntactically more convenient, how would you write an
assignment operator or copy constructor without using references?

john
Jul 19 '05 #3
>
Also a pointer is syntactically more convenient, how would you write an
assignment operator or copy constructor without using references?


I mean a REFERENCE is syntactically more convenient, of course.

john
Jul 19 '05 #4

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:bh******** ****@ID-196037.news.uni-berlin.de...

"David White" <no@email.provi ded> wrote in message
news:ta******** ********@nasal. pacific.net.au. ..
Web Developer <no****@hotmail .com> wrote in message
news:3f******** **@news.iprimus .com.au...
Hi,

Can someone provide a short and concise statement(s) on the difference
between pointers and references.
A pointer is an object containing the address in memory of another object; a
reference is an alias for another object.
A pointer can be null; a reference cannot.
A pointer can be re-seated (i.e., pointed at a different object); a
reference cannot.
A reference must be initialized (i.e., its refer-ee specified) when it

is created; a pointer can be uninitialized.


Also a pointer is syntactically more convenient, how would you write an
assignment operator or copy constructor without using references?

Also there is no such thing as a null reference. This could imply that it is
more efficient to use references because you need
not test the validity of a reference before using it.
Pointers, on the other hand, should generally be tested against null.
Jul 19 '05 #5
"Josephine Schafer" <js*@usa.net> wrote in message
news:bh******** ****@ID-192448.news.uni-berlin.de...
"David White" <no@email.provi ded> wrote in message
news:ta******** ********@nasal. pacific.net.au. ..
A pointer can be null; a reference cannot.
Also there is no such thing as a null reference.


You must have missed it :)

DW

Jul 19 '05 #6
> > Hi,
> >
> > Can someone provide a short and concise statement(s) on the difference > > between pointers and references.
>
> A pointer is an object containing the address in memory of another object;
a
> reference is an alias for another object.
> A pointer can be null; a reference cannot.
> A pointer can be re-seated (i.e., pointed at a different object); a
> reference cannot.
> A reference must be initialized (i.e., its refer-ee specified) when it > is created; a pointer can be uninitialized.


Wait a minute, in Java, an object reference also hold the memory address

of an object. Can you understand my confusion? I think my background Java may
curse me in C++.


I think I got it now. A reference gets you the value it references, but for
object references it does not give you the actual value but the memory
address of where the value lies, and in this latter case, it is EXACTLY the
same as a pointer.

Summary: object references in Java are pointers - they are just not
explicitly defined.

Note: I realise pointers don't behave like object references in Java.
Any comments appreciated.
Regards
WD

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
Jul 19 '05 #7
> I think I got it now. A reference gets you the value it references,
but for
object references it does not give you the actual value but the memory
address of where the value lies, and in this latter case, it is EXACTLY the same as a pointer.

Summary: object references in Java are pointers - they are just not
explicitly defined.


Java references are closer to C++ pointers than to C++ references. A
difference between Java references and C++ pointers is that you can
perform arithmetric operations on C++ pointers e.g.:

int* a = new int[10];
int* b = a + 1;
int* c = b - 1;

*b = 2; // Modifies a[1]
*c = 1; // Modifies a[0]

However typically you want to avoid pointer arithmetric where ever you
can. If the pointer goes outside allocated memory sooner or later (if
you are unlucky) nasty things will happen.

It looks like you are trying to learn C++ in terms of Java. Even though
the syntax of those languages look deceptively similar, they are really
different languages with different semantics and idioms. It is better to
start C++ with a clear and open mind. C++ often requires a different
approach; and trying to do things the Java way usually doesn't work out
too well in C++.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl

Jul 19 '05 #8
ghl
"Web Developer" <no****@hotmail .com> wrote in message
news:3f******** @news.iprimus.c om.au...
> > Hi,
> >
> > Can someone provide a short and concise statement(s) on the difference > > between pointers and references.
>
> A pointer is an object containing the address in memory of another object;
a
> reference is an alias for another object.
> A pointer can be null; a reference cannot.
> A pointer can be re-seated (i.e., pointed at a different object); a
> reference cannot.
> A reference must be initialized (i.e., its refer-ee specified) when it > is created; a pointer can be uninitialized.


Wait a minute, in Java, an object reference also hold the memory address

of an object. Can you understand my confusion? I think my background Java may
curse me in C++.


It isn't clear what is meant by "the memory address of an object." Since an
object is a conceptual fiction and consists in reality of various control
structures, arrays, member data, etc. it is unclear as to just what exactly
a reference variable points to. Probably most implementation use it to
address the start of the structure containing all the object information,
but I don't know that structure myself.

I also believe that a reference in C++ is not necessarily an address of the
data object, but could be implemented in some other fashion (say, a handle
that refers to an address of the data object). The only requirement is that
the identifier used for the reference can be used interchangeably for the
declaration name of the object being referenced. Probably most
implementations simply use the address of the data object. The restrictions
on the C++ reference prohibit using it like a pointer, which does contain
the address of the data object pointed to.

Like a pointer, the Java reference variable contains an address, but the
language syntax checks the type of the primitive or object being stored into
it for compatibility. So, a Java reference cannot be used as a pointer; same
as in C++. In Java a reference variable can be set to null.

Since I am here to learn C++: can a reference in C++ be set to null? I
should think not, since it only stands for one object and cannot be changed.

Yes, these languages are different. It's a pity (in a way) that they use the
terms "reference" and "object" so differently.
--
Gary

Jul 19 '05 #9
> Since I am here to learn C++: can a reference in C++ be set to null? I
should think not, since it only stands for one object and cannot be changed.

You think right; C++ references cannot be 0, if were posible it would be
useless since C++ references cannot be reseated.
Yes, these languages are different. It's a pity (in a way) that they use the terms "reference" and "object" so differently.


I think it is a pitty Java plagiarises the C++ syntax while at the same time
having different semantics. This leads to unnecessary confusion for people
moving from C++ to Java and visa versa.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 19 '05 #10

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

Similar topics

4
4959
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers to. My question is: In what situations is it better to use a reference variable over a pointer? Also, are there other differences besides the ones I listed above?
26
2473
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 pointer? My workplace discourages the use of pointers when it comes to parameter passing. They...
7
1890
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false; string value3 = "Hello Detroit"; public class x {
12
2277
by: arganx | last post by:
Before I had a good idea how to use pointers, I was wondering - first, how to use them. And second, of what value would they be. Now that I have at least a pretty good idea of how to use them, I am still wondering of what real value are they? I'll admit - its pretty sexy to be able to assign values indirectly, but what I'm trying to figue out is why it is any better than assigning the values directly.
458
21533
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
7
1518
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
64
3441
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like pointers. Can someone please explain why? thanks. Zytan
19
1994
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not references. Yet, in an interview, he laments the overuse of pointers and such in code. It seems contradictory to me. Why should we not use references instead of pointers? Can someone give me an idea of best practices in this respect? regards,...
29
1521
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
0
9705
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
10568
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...
1
10311
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,...
0
10074
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
9138
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...
0
6847
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();...
1
4292
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
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.