473,378 Members | 1,462 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,378 software developers and data experts.

What is the requirement of references in C++?

Why do we need references in C++ when we can accomplish the same task with pointers?
Putting it in other words:
What are the advantages of using references over pointers?

How actually is passing using references in C++ is faster and efficient than passing values using pointers?
Aug 27 '10 #1
3 1269
weaknessforcats
9,208 Expert Mod 8TB
References are passed as pointers. The compiler will de-reference the pointer for you.

The advantage of references over pointers is that when you use pointers that you dereference yourself you will at some point screw up. The compiler never screws up.

Second, studies have shown that member syntax is easy to use by programmers but they trip over pointer sytax:

Easy:

Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2.     fx(a);
  3.  
  4. void fx(int& arg)
  5. {
  6.    arg = 20;
  7. }
Not so easy:

Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2.     fx(&10);
  3.  
  4. void fx(int* arg)
  5. {
  6.     *arg = 20;
  7. }
Third, a function argument that uses an object requires that a copy of the object by made. That is, the copy constructor must be called. To avoid the copy, you use pointers and pass the address of the object and this gets you the "not so easy" case. Using a reference avoids the copy and get you the "easy to use" case.

Fourth, you use references as function arguments. Nowhere else are you to use references.

Fifth, you use a reference argument if the function does not need to switch to another object. You can't change a reference to refer to another object. If your function needs to change the object, then you use a pointer.

Sixth, review your code. If the function has a pointer argument and never changes the address in the pointer, then change the argument to a reference. You will find this eliminates 90% of the pointers in your code.
Aug 27 '10 #2
whodgson
542 512MB
Well there you go!.....and here I was, previously believing that a reference was simply another name for a declared variable full stop.
Aug 30 '10 #3
donbock
2,426 Expert 2GB
Not so easy indeed!
Note the change to the argument when fx is called.
Expand|Select|Wrap|Line Numbers
  1. int a = 10; 
  2.     fx(&a); 
  3.  
  4. void fx(int* arg) 
  5.     *arg = 20; 
Aug 30 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
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...
8
by: apoorv | last post by:
can anybody explain me how exactly C++ lang implements references.I read it from some where that it is nothing but const pointer internally but few of my seniors told me its not that than what is...
6
by: BR | last post by:
Is there a way to tell what exe is using which version of a .dll in the gac?
2
by: Simon | last post by:
If there is a different newsgroup for specific Visual Studio questions then please let me know - in the meantime perhaps I can post the question here: It seems that whenever I create new...
11
by: codebloatation | last post by:
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?
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: darrel | last post by:
Our CMS sets a cookie when you login. Various applications within the CMS check the cookie to establish the user's name, permissions, etc. The class below is what is references whenever we need...
29
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
0
by: Irfy | last post by:
Could someone elaborate on the purpose and concrete usage scenarios of references to functions. What can references to functions do that pointers to functions cannot. Why are they in C++? A swap...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.