Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference vs pointer .. ?

Reply
Thread Tools

reference vs pointer .. ?

 
 
m sergei
Guest
Posts: n/a
 
      07-01-2004
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-01-2004
On 30 Jun 2004 21:06:34 -0700, m sergei <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      07-01-2004
> 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


 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      07-01-2004
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<(E-Mail Removed)-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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Unforgiven
Guest
Posts: n/a
 
      07-01-2004
"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:(E-Mail Removed)...
> On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
> <(E-Mail Removed)-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

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      07-01-2004
On Thu, 1 Jul 2004 11:35:28 +0200, "Unforgiven"
<(E-Mail Removed)> wrote:

>"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
>news:(E-Mail Removed).. .
>> On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
>> <(E-Mail Removed)-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
(E-Mail Removed)
 
Reply With Quote
 
m sergei
Guest
Posts: n/a
 
      07-01-2004
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-01-2004

"m sergei" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-01-2004
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.

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-01-2004

int& k = ... ;

(&k)[5] = 34;


-JKop
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments