473,804 Members | 3,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4959
In article <kj************ *************** *****@4ax.com>,
cppaddict <he***@hello.co m> 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*******@pres by.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********@pobo x.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #3
cppaddict <he***@hello.co m> 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
3116
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 initializer lists have to be cut and pasted from one constructor to another. This does not seem to lend itself particularly well to maintainablility. Calling a constructor from another in C++ is not legal unlike in Java. Also, functions other...
2
1285
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
1629
by: Chaos | last post by:
Is It possible to have reference variables like in PHP ex. <?php $x = 1; $y =& $x; $y += 1;
16
2393
by: jryden | last post by:
I can do this: struct myInt { int i; }; void func1(myInt &i = myInt()); but not this:
3
1523
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
2021
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 loop (easy) waiting for the mouse to be clicked (I dont need help with this) and depending on user input a variable might define what function I want (be it line, circle....(again these are my issues)
3
1410
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 a=5,b=6;
9
2340
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
0
9577
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,...
0
10569
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...
0
10325
jinu1996
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...
1
10315
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
9140
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...
1
7615
isladogs
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...
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();...
0
5519
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...
1
4295
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 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.