CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 1999
    Location
    PA
    Posts
    38

    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.

  2. #2
    Join Date
    May 1999
    Posts
    53

    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.




  3. #3
    Join Date
    May 1999
    Location
    Piacenza, ITALIA
    Posts
    30

    Re: What (*&) means?

    what is the difference to :

    int n = 10;
    printf("%d", n );

    ??????????????????????????????????????


  4. #4
    Join Date
    May 1999
    Posts
    53

    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.




  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

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




  6. #6
    Guest

    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




  7. #7
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    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 *&




  8. #8
    Guest

    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


  9. #9
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    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


  10. #10
    Join Date
    Apr 1999
    Posts
    90

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


  11. #11
    Join Date
    May 1999
    Posts
    8

    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
  •  





Click Here to Expand Forum to Full Width

Featured