Click to See Complete Forum and Search --> : References versus pointers
Brendan Cullen
April 28th, 1999, 11:40 AM
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
mdwilliams
April 28th, 1999, 01:00 PM
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
Paul McKenzie
April 28th, 1999, 04:08 PM
Copy constructor:
class A {
public:
A(const A&);
};
Regards,
Paul McKenzie
sally
April 28th, 1999, 07:30 PM
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
Sally
April 28th, 1999, 07:30 PM
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
Brendan Cullen
April 29th, 1999, 02:24 AM
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
Michael Decker
April 29th, 1999, 08:35 AM
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
April 29th, 1999, 09:55 AM
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.
Brendan Cullen
April 29th, 1999, 10:03 AM
Thanks Yash,
Brendan
Brendan Cullen
April 29th, 1999, 10:04 AM
Thanks Michael,
Brendan
Brendan Cullen
April 29th, 1999, 10:06 AM
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
Brendan Cullen
April 29th, 1999, 10:08 AM
Thanks Matt,
Brendan
June 14th, 1999, 11:55 PM
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.
June 15th, 1999, 12:21 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.