473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Jul 22 '05 #1
20 4331
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
> 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
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
"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
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
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

"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
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

int& k = ... ;

(&k)[5] = 34;
-JKop
Jul 22 '05 #10
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
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
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
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
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
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
Bob Hairgrove posted:
If this isn't trolling, tell me, what is??

*plonk*

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

This.
-JKop
Jul 22 '05 #17

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.