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.processF oo( 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 3 1997
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.processF oo( 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.processF oo( *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.
"Scott" <sc***@nospam.c om> wrote in message
news:pa******** *************** *****@nospam.co m... 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.processF oo( 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...sinc e '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.processF oo( *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
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.processF oo( *pFoo );
Thanks to both responders. That was the ticket!
Best,
Scott This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- if T is a scalar type (3.9), the object is set to the value of 0 (zero)
converted to T;
|
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 tell me which one will be more efficient and why?
as far as i know both must be same, because i read somewhere that internally
|
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 WIZEditHousehold
closes).
A reference to a buffer within UIHousehold is supplied (passed by REF)
to the constructor of WIZEditHousehold - that constructor saves the
reference to the buffer in a member variable.
|
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
which is part of the "atoms" structure that is passed into the function
from the outside. I have not yet found where it is allocated but I'm
reasonably sure from other chunks of this code that it was by:
|
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:
| |
by: Kuku |
last post by:
What is the difference between a reference and a pointer?
|
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????
|
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.
My observation:
1. Compiler does not complain.
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |