473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pointers VS reference variables

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?

Thanks,
cpp
Jul 22 '05 #1
4 4930
In article <kj********************************@4ax.com>,
cppaddict <he***@hello.com> wrote:
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?


Define "better". Does it mean "simpler syntax"? Then references are
better (fewer pesky &'s and *'s to worry about). Does it mean "having
things out in the open so you can see explicitly what's going on?" Then
pointers are better, because the &'s and *'s give you clues as to what's
likely to happen to the data.

Except for those situations that *require* one or the other, using
pointers versus references is basically a religious-type issue. I
personally prefer to use references unless I have to use pointers (e.g.
when I need to be able to change which object it refers to). Some people
have the opposite preference.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2
cppaddict wrote:
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?
This is addressed in the FAQ:

http://www.parashift.com/c++-faq-lit...s.html#faq-8.6
Also, are there other differences besides
the ones I listed above?


Well, to get at the data to which a pointer points, you have to
dereference it (with operators like *, ->, or []). You don't have to do
anything like that with references.

Also, with pointers you can do pointer arithmetic. Not so with references.

And pointers allow multiple levels of indirection--pointers to pointers,
pointers to pointers to pointers, etc. That's not the case with
references; you can't declare something as a "reference to a reference"
(or even "pointer to a reference), and if you initialize a reference
with another reference, you really just end up with a reference to the
original object. As I recall, the C++ standard says references are
"names of objects," not objects themselves (unlike pointers).

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #3
cppaddict <he***@hello.com> wrote in message news:<kj********************************@4ax.com>. ..
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?
In very simple words
--when u know u have an object to refer to and also u'll never want to
refer to anything else other then that object use reference else use
pointer.
--use reference Also if you are using constants.
--certain operator implementation requires you to use references.
Also, are there other differences besides
the ones I listed above?

Thanks,
cpp

Jul 22 '05 #4
Pointers may be null.

References may not be null.

Say you are writing a function and you know that you do not wish an argument to
be an object of type T.

That leaves you with two choices. The argument can be a pointer to T or a
reference to T.

If it is acceptable for the caller of the function to pass nothing, either
because he has nothing or because nothing can be used to indicate special
processing,
then use a pointer because it can be initialized with a null value.

If it is not acceptable for the caller of the function to pass nothing, then
use a reference because it cannot be initialized with a null value.

Enforce contracts at compile-time rather than at run-time wherever possible.

I have been looking at another object-oriented programming language as of late
and one of the things I don't like about it is that it only allows access to
objects through its version of references in a world where a reference can
always refer to nothing. Thus much of the code I have seen is littered with
statements such as (if ref!=null) or the code just throws exceptions at
run-time if a null reference is used.

IMHO the C++ methods for accessing objects indirectly are more diverse than
this other language and the slight difference between pointers and references
in C++ allow the programmer to communicate intent to fellow programmers with
the advantage of support of the compiler.


Jul 22 '05 #5

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

Similar topics

2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
2
by: Dmitri Islentyev | last post by:
Hello everyone! Please consider the following code: class Base { public: Base(int x); virtual int foo() { return 17; } };
5
by: Chaos | last post by:
Is It possible to have reference variables like in PHP ex. <?php $x = 1; $y =& $x; $y += 1;
16
by: jryden | last post by:
I can do this: struct myInt { int i; }; void func1(myInt &i = myInt()); but not this:
3
by: v4vijayakumar | last post by:
While we can do whatever we want to do with the member variables, with the member references, why member references are not common? Note: This is not homework question. :)
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
3
by: Rahul | last post by:
Hi Everyone, I just tried the following code, in response to some post on reference member variable and i'm not able to understand what exactly is happening over here, int main() { int...
9
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.