By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

reference vs pointer .. ?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On 30 Jun 2004 21:06:34 -0700, m sergei <se********@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
Jul 22 '05 #2

P: n/a
> 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
Jul 22 '05 #3

P: n/a
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<nd*@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
No**********@Home.com
Jul 22 '05 #4

P: n/a
"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:jd********************************@4ax.com...
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<nd*@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

Jul 22 '05 #5

P: n/a
On Thu, 1 Jul 2004 11:35:28 +0200, "Unforgiven"
<ja*******@hotmail.com> wrote:
"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:jd********************************@4ax.com.. .
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<nd*@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
No**********@Home.com
Jul 22 '05 #6

P: n/a
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

Jul 22 '05 #7

P: n/a

"m sergei" <se********@yahoo.com> wrote in message
news:86**************************@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
Jul 22 '05 #8

P: n/a
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.

Jul 22 '05 #9

P: n/a

int& k = ... ;

(&k)[5] = 34;
-JKop
Jul 22 '05 #10

P: n/a
JKop wrote:

int& k = ... ;

(&k)[5] = 34;


You don't actually use the reference for indexing, but rather take the
pointer to the int it refers to (&k) and use that. Sure, you could also
do:

int& k = *new int[100];
(&k)[5] = 34;
delete &k;

but it's not very useful or intuitive.

Jul 22 '05 #11

P: n/a
On Thu, 01 Jul 2004 13:39:55 GMT, JKop <NU**@NULL.NULL> wrote:

int& k = ... ;

(&k)[5] = 34;
-JKop


What is that supposed to illustrate?

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #12

P: n/a
Bob Hairgrove posted:
On Thu, 01 Jul 2004 13:39:55 GMT, JKop <NU**@NULL.NULL> wrote:

int& k = ... ;

(&k)[5] = 34;
-JKop


What is that supposed to illustrate?

--
Bob Hairgrove
No**********@Home.com

Well it's more entertaining than the "variable" conversation that's going
on.

-JKop

Jul 22 '05 #13

P: n/a
JKop wrote:
Well it's more entertaining than the "variable" conversation that's
going on.


So you're here for entertainment? If you want that, you should try any
newsgroup that contains "advocacy" in its name.

Jul 22 '05 #14

P: n/a
On Thu, 01 Jul 2004 16:31:40 GMT, JKop <NU**@NULL.NULL> wrote:
Bob Hairgrove posted:
On Thu, 01 Jul 2004 13:39:55 GMT, JKop <NU**@NULL.NULL> wrote:

int& k = ... ;

(&k)[5] = 34;
-JKop


What is that supposed to illustrate?

--
Bob Hairgrove
No**********@Home.com

Well it's more entertaining than the "variable" conversation that's going
on.

-JKop


If this isn't trolling, tell me, what is??

*plonk*

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #15

P: n/a
m. sergei :
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 found two pages on this topic:

http://www.seasite.niu.edu/cs240/CPP_Notes/Notes9.htm
http://www.phptr.com/articles/articl...31783&seqNum=3

According to the latter, the use of references is handier because the
program is easier to read and write, but this is only a cosmetic
difference.

--
`cat ~/.signature`
Jul 22 '05 #16

P: n/a
Bob Hairgrove posted:
If this isn't trolling, tell me, what is??

*plonk*

--
Bob Hairgrove
No**********@Home.com

This.
-JKop
Jul 22 '05 #17

P: n/a

"m sergei" <se********@yahoo.com> wrote in message
news:86**************************@posting.google.c om...
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 ?


Basically, a pointer is more complicated than you need. Use a reference
when you can, and a pointer only when a reference won't work.
Jul 22 '05 #18

P: n/a
say class is Test
then
Test *t=new Test();
t->a=9; //to access a member of class Test
how would the & reference work here (instead of *t that was used above)

like Test &t = new Test() //gives compile error

please give the correct usage with & (reference of object)
Jul 22 '05 #19

P: n/a
m sergei wrote:
say class is Test
then
Test *t=new Test();
t->a=9; //to access a member of class Test
how would the & reference work here (instead of *t that was used above)

like Test &t = new Test() //gives compile error

please give the correct usage with & (reference of object)


Test & t = * new Test ();
t.a = 9;
delete & t;

--
Regards,
Buster.
Jul 22 '05 #20

P: n/a
Rolf Magnus wrote:
[...] Sure, you could also do:

int& k = *new int[100];
(&k)[5] = 34;
delete &k;
delete [] & k;
but it's not very useful or intuitive.


Agreed.

--
Regards,
Buster.
Jul 22 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.