reference vs pointer .. ?

M

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 had posted a similar question in the C lang newsgroup but did not
get much help as C does not do references to objects.
 
J

John Harrison

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
 
N

Niels Dybdahl

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
 
B

Bob Hairgrove

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.
 
U

Unforgiven

Bob Hairgrove said:
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].
 
B

Bob Hairgrove

Bob Hairgrove said:
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.
 
M

m sergei

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 ?)
 
J

John Harrison

m sergei said:
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
 
R

Rolf Magnus

m said:
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.
 
R

Rolf Magnus

JKop said:
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.
 
R

Rolf Magnus

JKop said:
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.
 
A

Apokrif

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/article.asp?p=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.
 
J

jeffc

m sergei said:
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.
 
M

m sergei

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)
 
B

Buster

m said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top