Q . When to use use pointer-to-pointer and reference-to-pointer to
modify a pointer passed to a function ?
If you structure your code so that the assignment doesn't happen until
the function is about to return, then there's not much difference in
readability between the two, e.g.:
void PtrToPtr(int **ppint) {
int *pint = new int[50];
pint[0] = 1234;
// etc etc
*ppint = pint;
}
void RefToPtr (int *&ppint) {
int *pint = new int[50];
pint[0] = 1234;
// etc etc
ppint = pint;
}
The only difference is the parameter type and the final assignment.
Also, structuring your code this way means that if your function
returns/throws early because of an error, you can leave the input
value unmodified, which is sometimes desirable. It can also make
cleanup and exception-safety very easy, consider:
void PtrToPtr (Object **pobj) {
std::auto_ptr<Object> obj(new Object);
// do some stuff that may throw, then:
*pobj = obj.release();
}
void RefToPtr (Object *&pobj) {
... same code, then ...
pobj = obj.release();
}
The unwanted Object is automatically freed on early return/throw,
until it is actually assigned to the output parameter.
Then, which way you do it is just a merely of personal style
preference.
That said, I sort of agree with Pascal's reply in that using a
reference to pointer might obscure the fact that it's an output
parameter at the call site. On the other hand, a simple check of the
function declaration would show that it accepts a non-const reference,
and I any non-const reference parameter passed to a function should
generally be assumed to be a candidate for modification, IMHO.
Why Pointer to Pointer is generally used ?
1. Pointer to pointer can be used in C and C++ but reference to
pointer is limited to C++.
I don't think this is a good line of reasoning without context. It's
true that references can only be used in C++ but that is only a
disadvantage if your code is also meant to be compiled by a C compiler
-- but in that case, you're probably coding in C, not C++. Just use
whatever features the language you are actually coding in supports. If
you are coding in C++ then there's no reason to stick to C because,
hey, you *are* writing C++ after all, not C.
a. Generally people start learning programming from C . So they
get used to pointer to pointer.
Incidentally, I taught myself C using a C++ compiler, and it really
made a mess of me!
But again, I also don't think this is a very
compelling reason. Whether or not you are used to doing something some
way in C should have no bearing on the way you do it in C++, as they
are entirely different languages, even though they happen to share a
lot in common.
b.Pointer to Pointer usage makes project porting in
C or C++ easy.
If you are porting from C to C++ then, of course, you wouldn't be
using references anyways.
If you are porting from C++ to C then you really have to ask yourself
if you are doing the right thing. I can't think of any situations
where you'd have to do that off the top of my head... maybe if you're
porting to a platform that doesn't have a C++ compiler available? But
what kind of application would be written in C++ then, surprise,
ported to a platform without a C++ compiler?
2. Generally libraries uses pointer to pointer approach like COM uses
pointer to pointer for CoCreateInstance or QueryInterface.
This is not true. It is more accurate to say that C libraries use
pointers to pointers. Also, about COM... COM itself doesn't specify
references or pointers or anything like that. COM has bindings in many
languages, e.g. VisualBASIC (where pointers vs. references don't apply
at all). You're probably used to seeing the C bindings. Windows API
functions like CoCreateInstance do not use references because they are
designed to be used by both C and C++ applications. Also COM doesn't
make any statements about class hierarchy relations, hence all the
casts to (void **) in QueryInterface et. al, you can't handle that
with only references.
You may want to have a read here:
http://en.wikipedia.org/wiki/Component_Object_Model
Another quality of references is the inability to represent NULL as a
value.
Fourm : Could you please let me know if these are the only reasons or
i am missing something?
None of those are reasons, I don't think. In the implementations of
the functions it does not make much of a difference. When the
functions are called, sometimes pointers-to-pointers are clearer,
sometimes maybe not, personally I look at const vs. non-const rather
than the presence of an "&". Reasons related to C are not generally
sound as C is a different language. I think a lot of it is style.
I also think that consistency is the most important thing. Pick one or
the other. If you have a strange mix of both pointers-to-pointers and
references-to-pointers serving the same purpose, *then* readability
becomes an issue.
J
Thanks
Vikrant
Example
#include "stdafx.h"
void Ref2Poi(int * &p)
{
*p = 10;
}
void Poi2Poi( int ** p)
{
**p = 10;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 13;
int j = 14;
int *p = &i;
int *q = &j;
Ref2Poi(p);
Poi2Poi(&q);
return 0;
}