473,511 Members | 15,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer and reference confusion

Hello,

I am having some difficulty with the following C++:

I have a declared a pointer to Foo like so:

Foo *pFoo;

Now, I have an API object that I try to use like so:

fooAPI.processFoo( pFoo );

Then, I get a compiler error that the prototype for processFoo is
expecting a Foo&, but is instead getting a Foo*&. So, my question is: how
do I transform my *pFoo to a Foo&, and perhaps someone could also help me
understand exactly what the difference between the two is? I hope this is
a least somewhat clear.

Thank-you.
Scott
May 25 '06 #1
3 1986

Scott wrote:
Hello,

I am having some difficulty with the following C++:

I have a declared a pointer to Foo like so:

Foo *pFoo;

Now, I have an API object that I try to use like so:

fooAPI.processFoo( pFoo );

Then, I get a compiler error that the prototype for processFoo is
expecting a Foo&, but is instead getting a Foo*&. So, my question is: how
do I transform my *pFoo to a Foo&, and perhaps someone could also help me
understand exactly what the difference between the two is? I hope this is
a least somewhat clear.


I believe the function is expecting a reference to the object, and not
a pointer to the object.
If you have a pointer, you can pass it by dereferencing it using *.
Example:
fooAPI.processFoo( *pFoo );

However, you should make sure that pFoo is pointing to a valid object.
Functions that take a reference, will usually expect to be getting a
valid initialized object, and not a NULL value.
Where as functions that take a pointer may expect the pointer to be
NULL.

In C++, experts recommend using reference over pointers when possible,
because it's less ambiguous in general.

May 25 '06 #2

"Scott" <sc***@nospam.com> wrote in message
news:pa****************************@nospam.com...
Hello,

I am having some difficulty with the following C++:

I have a declared a pointer to Foo like so:

Foo *pFoo;

Now, I have an API object that I try to use like so:

fooAPI.processFoo( pFoo );

Then, I get a compiler error that the prototype for processFoo is
expecting a Foo&, but is instead getting a Foo*&. So, my question is: how
do I transform my *pFoo to a Foo&, and perhaps someone could also help me
understand exactly what the difference between the two is? I hope this is
a least somewhat clear.


A reference works *sort of* like a pointer, but without the pointer syntax.
However, unlike a pointer, a reference must always refer to an object
(pointers can be NULL, references cannot), so for example, it is illegal to
write:

int & ref; // illegal -- 'ref' does not refer to anything.

Instead, you would have to write:

int i;
int & ref = i;

In this case, 'ref' refers to 'i' and for all intents and purposes 'i' and
'ref' are the same variable, representing the same space in memory, and both
may be used in the same ways. Thus, when writing:

ref = 6;
i = 6;

....both assignments above accomplish the exact same thing, that is,
assigning the value 6 to the variable 'i'.

Now, to your main question...since 'processFoo' expects a reference to a
'Foo' (Foo&), you cannot pass it a pointer, so you must dereference the
pointer so that the function gets a Foo instead of a Foo*, like this:

fooAPI.processFoo( *pFoo );

Of course, 'pFoo' must be pointing to something reasonable, or dereferencing
it will result in a nasty crash. But then you probably already knew that.

HTH,

- Dennis
May 25 '06 #3
On Wed, 24 May 2006 22:47:21 -0700, Axter wrote:
I believe the function is expecting a reference to the object, and not
a pointer to the object.
If you have a pointer, you can pass it by dereferencing it using *.
Example:
fooAPI.processFoo( *pFoo );


Thanks to both responders. That was the ticket!

Best,
Scott

May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4716
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
9
2977
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...
6
1500
by: TZ | last post by:
I have two classes, UIHousehold and WIZEditHousehold (both are winforms). UIHousehold creates and shows the WIZEditHousehold (WIZEditHousehold can't be modal, so UIHousehold doesn't know when...
3
2132
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
6
10613
by: Scott | last post by:
Hi All, I can't seem to wrap my head around this one. I have a pointer, int *x; which I can assign:
51
4394
by: Kuku | last post by:
What is the difference between a reference and a pointer?
42
5276
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
5
1681
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. ...
24
1934
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
0
7252
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
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
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,...
1
5077
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.