Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   reference vs pointer .. ? (http://www.velocityreviews.com/forums/t284113-reference-vs-pointer.html)

m sergei 07-01-2004 04:06 AM

reference vs pointer .. ?
 
Question is about the difference between these two

reference to an object versus pointer to an object

What are advantages in either one and when would one use them. I see
lots of examples when an object is passed as a parameter to a function
it is passed as a reference. Though it would have been fine to pass it
as a pointer as well ? is that right ?
I had posted a similar question in the C lang newsgroup but did not
get much help as C does not do references to objects.

John Harrison 07-01-2004 05:18 AM

Re: reference vs pointer .. ?
 
On 30 Jun 2004 21:06:34 -0700, m sergei <sergei_m_1@yahoo.com> wrote:

> Question is about the difference between these two
>
> reference to an object versus pointer to an object
>
> What are advantages in either one and when would one use them. I see
> lots of examples when an object is passed as a parameter to a function
> it is passed as a reference. Though it would have been fine to pass it
> as a pointer as well ? is that right ?


It would work if passed as a pointer. But

1) A pointer can be NULL, so the function must test for that.

2) A pointer cannot be initialised with a temporary, so you lose
flexibility

X f();
void g(const X&);
g(f()); // fine

X f();
void g(const X*);
g(&f()); // compile error


> I had posted a similar question in the C lang newsgroup but did not
> get much help as C does not do references to objects.


Not surprising really.

john

Niels Dybdahl 07-01-2004 08:23 AM

Re: reference vs pointer .. ?
 
> 1) A pointer can be NULL, so the function must test for that.
>
> 2) A pointer cannot be initialised with a temporary, so you lose
> flexibility


and

3) A pointer can be indexed while a reference can not. So by using a
reference you implicit state that this is not an array.

4) The syntax for using a reference is the same as for using an object on
the stack, so it is easier to rewrite code that previously worked on a stack
object to use a reference than to use a pointer.

Niels Dybdahl



Bob Hairgrove 07-01-2004 08:43 AM

Re: reference vs pointer .. ?
 
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<ndy@fjern.detteesko-graphics.com> wrote:

>and
>
>3) A pointer can be indexed while a reference can not. So by using a
>reference you implicit state that this is not an array.


Not necessarily.

For references to built-in types, this is true. However, user-defined
types can overload operator[], and you can of course index a reference
to a pointer of any type. The mere fact that you have a reference
doesn't automatically prohibit the use of operator[] from the
standpoint of the grammar.


--
Bob Hairgrove
NoSpamPlease@Home.com

Unforgiven 07-01-2004 09:35 AM

Re: reference vs pointer .. ?
 
"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:jdj7e0pg1k0rkrpoc3i4aut1c7v7sojgkg@4ax.com...
> On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
> <ndy@fjern.detteesko-graphics.com> wrote:
>
>>and
>>
>>3) A pointer can be indexed while a reference can not. So by using a
>>reference you implicit state that this is not an array.

>
> Not necessarily.
>
> For references to built-in types, this is true. However, user-defined
> types can overload operator[], and you can of course index a reference
> to a pointer of any type. The mere fact that you have a reference
> doesn't automatically prohibit the use of operator[] from the
> standpoint of the grammar.


No, but if you have a type that defines operator[], using [] on a pointer to
it would index the pointer, while using it on a reference indexes the
original type. Using a pointer in such a scenario can lead to subtle bugs,
and at the very least ugly syntax such as (*somevar)[2].

--
Unforgiven


Bob Hairgrove 07-01-2004 10:21 AM

Re: reference vs pointer .. ?
 
On Thu, 1 Jul 2004 11:35:28 +0200, "Unforgiven"
<jaapd3000@hotmail.com> wrote:

>"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
>news:jdj7e0pg1k0rkrpoc3i4aut1c7v7sojgkg@4ax.com.. .
>> On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
>> <ndy@fjern.detteesko-graphics.com> wrote:
>>
>>>and
>>>
>>>3) A pointer can be indexed while a reference can not. So by using a
>>>reference you implicit state that this is not an array.

>>
>> Not necessarily.
>>
>> For references to built-in types, this is true. However, user-defined
>> types can overload operator[], and you can of course index a reference
>> to a pointer of any type. The mere fact that you have a reference
>> doesn't automatically prohibit the use of operator[] from the
>> standpoint of the grammar.

>
>No, but if you have a type that defines operator[], using [] on a pointer to
>it would index the pointer, while using it on a reference indexes the
>original type. Using a pointer in such a scenario can lead to subtle bugs,
>and at the very least ugly syntax such as (*somevar)[2].


That's true, but that certainly wasn't what I was referring to. The
issue was whether one could index a reference or not. I wasn't talking
about indexing into a pointer, except in the context of reference to
pointer.

Yet another reason to use STL container classes and avoid arrays and
pointers if possible.

--
Bob Hairgrove
NoSpamPlease@Home.com

m sergei 07-01-2004 12:19 PM

Re: reference vs pointer .. ?
 
void g(const X&) why is const required ? is that a requirement to inform the
compiler ?


also your second function void g(const X*).
is void g(const *X) same as above ? (or correct ?)

> It would work if passed as a pointer. But
>
> 1) A pointer can be NULL, so the function must test for that.
>
> 2) A pointer cannot be initialised with a temporary, so you lose
> flexibility
>
> X f();
> void g(const X&);
> g(f()); // fine
>
> X f();
> void g(const X*);
> g(&f()); // compile error


John Harrison 07-01-2004 12:49 PM

Re: reference vs pointer .. ?
 

"m sergei" <sergei_m_1@yahoo.com> wrote in message
news:86bb865e.0407010419.7113f3a6@posting.google.c om...
> void g(const X&) why is const required ? is that a requirement to inform

the
> compiler ?
>
>
> also your second function void g(const X*).
> is void g(const *X) same as above ? (or correct ?)
>


Only const references can be bound to temporaries

X f();
void g(const X&);
void h(X&);

g(f()); // legal
h(f()); // illegal

Not all compilers enforce this rule however.

The pointer case is illegal with or without const however.

john



Rolf Magnus 07-01-2004 01:19 PM

Re: reference vs pointer .. ?
 
m sergei wrote:

> void g(const X&) why is const required ? is that a requirement to
> inform the compiler ?


It tells the compiler (as well as the programmer) that your function
doesn't modify the object it gets passed. Without it, you can't pass
constant objects to the function, and you can't pass temporary objects.

> also your second function void g(const X*).
> is void g(const *X) same as above ? (or correct ?)


No, it's not correct. * has to follow the name of a type to show that
you want a pointer to that type. 'const' is not a type.


JKop 07-01-2004 01:39 PM

Re: reference vs pointer .. ?
 

int& k = ... ;

(&k)[5] = 34;


-JKop


All times are GMT. The time now is 04:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.