non-redundant use of &*

This is a discussion on non-redundant use of &* within the C++ Programming forums, part of the General Programming Boards category; Up until now I considered it redundant, but I've just recently needed to use &* legitimately and was wondering if ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    non-redundant use of &*

    Up until now I considered it redundant, but I've just recently needed to use &* legitimately and was wondering if there are any other cases for its use?
    Code:
    void doStuff( Foo* p ){ ... }
    ...
    list<Foo> foos;
    for( list<Foo>::iterator it = foos.begin(); it != foos.end(); ++it )
     doStuff( &*it );

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could try

    Code:
    void doStuff(/*const?*/ Foo& p);
    However, an iterator is not a pointer, so "dereferencing" it and getting the address of the result doesn't give you the same thing back.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Quote Originally Posted by anon View Post
    However, an iterator is not a pointer, so "dereferencing" it and getting the address of the result doesn't give you the same thing back.
    I believe that's the point - otherwise &* would be redundant.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I believe that's the point - otherwise &* would be redundant.
    Exactly. It is non-redundant because the iterator is not a pointer as such - it PRETENDS to be a pointer by having an operator*() that returns the address of the actual object represented by the iterator proxy object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Ah I see, I forgot about overloaded ref/deref operators. I guess that answers the question then: in C++ you can always define &* to be a non-redundant operation...

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Indeed, but I wouldn't just about abuse it everywhere, because it breaks logic.
    It could especially confuse C programmers, I suppose, but then again, what C programmer scourges through and maintains C++ code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It could especially confuse C programmers, I suppose, but then again, what C programmer scourges through and maintains C++ code?
    Judging by some of the code we have here that come from third parties: Far too many!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    It was also a sort of half-sarcastic joke.
    I know there have been some C programmers who have maintained or read C++ code, which is actually why I added the meaning of precaution. Don't abuse &*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It was also a sort of half-sarcastic joke.
    I know there have been some C programmers who have maintained or read C++ code, which is actually why I added the meaning of precaution. Don't abuse &*.
    Sure, I understood it as a joke, but I also look at it from my view - there are some people who are not using C++ like they should.

    And abusing operators in general is something that should be done with care.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by Elysia View Post
    Indeed, but I wouldn't just about abuse it everywhere, because it breaks logic.
    It could especially confuse C programmers, I suppose, but then again, what C programmer scourges through and maintains C++ code?
    Actually, that's pretty much the reason for this thread! I'm primarily a C-programmer, and so immediately thought "&*" = redundant; until I was reminded of the shapeshifting abilities of overloading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 05:43 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 07:05 AM
  3. Parsing and Fwrite Help Please.
    By Freestyler in forum C Programming
    Replies: 11
    Last Post: 12-06-2005, 10:38 AM
  4. redundant function call
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 01-05-2005, 11:49 PM
  5. array problems
    By c++.prog.newbie in forum C++ Programming
    Replies: 10
    Last Post: 04-26-2004, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21