Solved

Function arguments: Pointers vs. References

Posted on 2006-06-23
10
248 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 (Megan) 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

861 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

23 Experts available now in Live!

Get 1:1 Help Now