References versus pointers
Hi,
If I want to pass the address of something to a function, why would it ever be better to use a reference instead of a pointer ? I'm aware of two possible reasons :
1 : I know that the called function is more legible if I use a reference. (I don't have to dereference a reference :-))
2 : If I decide that the called function requires not the address of the "something" but the value of the "something", I can make the change simply in the the function prototype.
What other advantages are there ?
Regards,
Brendan
Re: References versus pointers
One advantage of using a reference is that you won't be passed a NULL pointer.
Another advantage would be in easier coding of the called function, i.e. you don't have to dereference the pointer.
-- Matt
Re: References versus pointers
Copy constructor:
class A {
public:
A(const A&);
};
Regards,
Paul McKenzie
Re: References versus pointers
Remeber that a reference in essence is a pointer, but viewad as a variable
A reference is a pointer that can NEVER change its value and can NEVER be NULL
refeneces are GREAT to use
sally
Re: References versus pointers
Hi Sally,
Thanks for the input.
>>A reference is a pointer that can NEVER change its value....
Are you saying that a reference can only ever be the address of one thing ?
Regards,
Brendan
Re: References versus pointers
One of things to remember about references is that you have to pass the correct data type. Don't assume implicit type casting will work correctly.
For example, if you have the function:
void SomeFunction(long int &nVal);
Don't do this!:
short n;
SomeFunction(n); // passing a short for a reference to a long may be a problem
Re: References versus pointers
Hi,
Having passed the parameter as a pointer, not only the contents of that memory can be changed( which is reflected in the called function) but also the pointer can be made to point to any other memory location. Thus the pointer value ifself can be changed .
But having passed the parameter as a refernce , the pointer value( if U can call so) cannot be changed . Only the contents of that memory can be changed which is reflected in the called function.
Thus when we want to copy a big block from some other memory to the one we need , rather than copying that full block having passed the parameter as Reference . if we pass the paramter as a pointer just changing the value of that pointer should help but then remember now 2 pointers point to same memory location .
Yash.
Re: References versus pointers
Re: References versus pointers
Re: References versus pointers
Hi Paul,
The language defines copy constructors (and assignment operators) to use references - but that's not to say that they're the best things to use :-)
Thanks,
Brendan
Re: References versus pointers
Re: References versus pointers
Your question would be better if reversed. Why would you ever want to use a pointer instead of a reference?
Off the top of my head I can think of only a few...
1. You want to be able to pass NULL (can't be done with a reference)
2. You'll change the pointer inside the function. (say your passing a pointer to a buffer that you'll be scanning for some value and your using the passed pointer to scan with)
But if your not doing one of the above.. you should always use a reference.
Re: References versus pointers
You need not be any more concerned about passing the correct data type with a reference than with a pointer because in both cases the compiler won't let you pass the incorrect type. The snippet of code you posted, for example, won't compile.
Maybe you where trying to point out is the problem with code like...
class Foo
{
SomeFunction(int val);
SomeFunction(long* pLong);
};
The compiler will convert the long* into an int and call SomeFunction(int) instead of SomeFunction(long*). The same would probably be true for a long& but I didn't take the time to test it.