Solved

Function arguments: Pointers vs. References

Posted on 2006-06-23
10
239 Views
Last Modified: 2010-04-01
In these two function headers:

   void func1( int *pv )
   void func2( int &v )

- What are the major differences?
- What are the key benefits of using one instead of the other?
- Will 'v' in effect be a pointer?

Thanks
0
Comment
Question by:InteractiveMind
10 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 50 total points
ID: 16970305
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.

this may help, too : http://www.cs.nott.ac.uk/~pni/Papers/Tutorials/C++/ref-vs-ptr.html
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 16970616
Internally pointer and reference are implemented by the same way. On Assembly level function has pointer parameter.
However, there is significant difference in C++ compiler level. Reference allows to chain overloaded operators. For example, all overloaded << stream operators return ostream&. This allows to write:

cout << n << m << endl;

cout << n is handled by ostream operator << (int). This operator returms ostream&, which allows to apply the same ostream operator << (int) to the next parameter m etc. Learning overloaded operators, you can see that they return reference to class object, their last line is:
return *this;

This is main advantage of reference. You can see extensive using of references in different C++ libraries, like STL. For simple functions like func1 and func2 the only difference is syntax.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 50 total points
ID: 16971164
Experts like Herb Sutter, recommend preferring to use reference over pointer, because a reference argument is less ambiguous.

A function that takes a pointer, could be receiving a pointer to a single argument, or an array of objects.
You don't have this abiguity with a reference argument.
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 50 total points
ID: 16971437
0
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 50 total points
ID: 16971740
- What are the major differences?
Pointer creates a local variable, that holds the address of the parameter sent.
Reference means an alias

- What are the key benefits of using one instead of the other?
Pointer is compatible with C and many other languages and helpful while writing DLLs.
Also pointer always takes (considering 32 bit machine) 32 bits of memory.
Reference avoids creating another variable, as it is just another name for the original parameter

- Will 'v' in effect be a pointer?
No. v is a reference.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 30

Expert Comment

by:Axter
ID: 16971949
FYI:
>>Pointer is compatible with C

This is not that important when creating class methods, or pointers to class objects that C can't use anyway.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16972003
Where did I mention classes ?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16972127
>>Where did I mention classes ?

My comment was not meant to contradict your statement, but to give additional information.
Since this is the C++ topic area, I think it's important to point out that compatibility to C should not be a factor if you already have headers that are incompatible to C.

This point may not be obvious to a beginner, or to someone unfamiliar with C.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16972156
Use extern C :)
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16972506
Thanks very much all; 50 points to all participants.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now