Solved

Function arguments: Pointers vs. References

Posted on 2006-06-23
10
267 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 43

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 86
best sources to up-to-date in C++? 8 104
I could not set window to top 4 45
Can Live bindings change TGrid Cell Colour ? 1 34
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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