|
-
May 9th, 1999, 05:15 PM
#1
What (*&) means?
Hello gurus,
Can any one tell me what is the meaning of (*&) operator, and why to use it?
Thanks in advance.
Chetan
Practice makes the man perfect.
-
May 9th, 1999, 05:44 PM
#2
Re: What (*&) means?
*& means to deference the address of a variable. For example, you could do something like:
int n = 10;
printf("%d", (*&n));
Output will be 10.
Sometimes you may need a cast, because dereferencing the address of a variable loses it's type.
-
May 9th, 1999, 10:32 PM
#3
Re: What (*&) means?
what is the difference to :
int n = 10;
printf("%d", n );
??????????????????????????????????????
-
May 9th, 1999, 11:13 PM
#4
Re: What (*&) means?
By the time printf gets the value, it's the same as if (*&n) where used. & counteracts * acting
like nothing..
For example these will produce same output:
printf("%d", 1); // like printf("%d", n);
printf("%d", 1 + 1 - 1); // like printf("%d", *&n);
See?
It's more steps to get the same result.
-
May 10th, 1999, 02:37 AM
#5
Re: What (*&) means?
// A time to use this fancy thingie is in the following:
void CMyObject: eleteObject( CMyObject*& obj )
{
if (obj != NULL)
{
delete obj;
obj = NULL;
}
}
// after calling this function in the following:
CMyObject* pObj = new CMyObject;
CMyObject: eleteObject(pObj)
// pObj will be set to NULL....
-
May 10th, 1999, 09:02 AM
#6
Re: What (*&) means?
Hello,
Whats the difference between
void CMyObject: eleteObject( CMyObject*& obj )
and void CMyObject: eleteObject( CMyObject* obj )
because i guess both the functions can achieve the same results?
Thanks
-
May 10th, 1999, 09:37 AM
#7
Re: What (*&) means?
(*&) Is used when you want to pass by reference one pointer, so when you call this the "variable" that you pass by parameter will be modified ( not the content, the adress in wich this pointer point).
If you want I can make you a little example, Bye !
Braulio
PS.: Take a look at CMapStringToPtr, LookUp method, there you must *&
-
May 10th, 1999, 12:34 PM
#8
Re: What (*&) means?
Thanks Braulio,
Sorry for bothering you, but as you said if it is possible for you to give some example about (*&) that will be great help.
Thanks again
Chetan
-
May 10th, 1999, 02:02 PM
#9
Re: What (*&) means?
Hi Again !
One example ( is no so util, but I think explains how works this well)::
CMyPtr *Ptr = new CMyPtr;
the *Ptr points to for example 0x0034 ( memory)
then we have to functions:
BadDelete(CMyPtr *Pointer)
{
delete Pointer;
Pointer = NULL;
}
GoodDelete(CMyPtr *&Pointer)
{
delete Pointer;
Pointer = NULL;
}
What happens if we made:
CMyPtr *Ptr = new CMyPtr;
BadDelete(Ptr)
Then, the memory will freed, but the "Ptr" pointer will still point to 0x0034
and if we made...
CMyPtr *Ptr = new CMyPtr;
GoodDelete(Ptr)
Then the Ptr after all will be NULL
Why use this, normally, when you call a function that allocates new memory, returns you the pointer as return value of the function, but what about if need to return more pointers, or you want that the return value of your function would be BOOL for example ( take a look at the CMapStringToPtr class, at the Lookup method).
HTH, if you have more questions, please ask, Bye !
Braulio
-
May 10th, 1999, 02:29 PM
#10
Re: What (*&) means?
The difference is that with *& the function DeleteObject() can change the value of the parameter 'obj' and not just what its pointing to. The example given where the person was deleting the pointer and then assigning it to NULL is a good one.
You could also perform the opposite function and allocate memory and assign it to 'obj'.
Before references, you had to pass a pointer to a pointer (which was confusing).
-
May 10th, 1999, 03:30 PM
#11
Re: What (*&) means?
Thanks Braulio,
Your explaination helped me.
Chetan
Nothing is impossible.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|