Operator ->* in smart pointers

Discussion in 'C++' started by Dario Saccavino, Feb 4, 2008.

  1. It seems to me that operator->* is overloadable in C++ (just like
    operator->), and it should be useful to implement in smart pointer
    classes.

    However, I haven't found its implementation either in the standard
    weak_ptr class, or in any boost smart pointer. Was it intentionally
    left out of those headers?
    Actually there's a workaround: just write
    (ptr.operator->())->*memptr
    instead of
    ptr->*memptr
    But it looks very ugly. How am I supposed to use member pointers with
    smart pointers?

    Thank you for your attention

    Dario
     
    Dario Saccavino, Feb 4, 2008
    #1
    1. Advertising

  2. On 4 Feb, 17:12, Dario Saccavino <> wrote:
    >
    > ...either in the standard weak_ptr class...


    Typo. I clearly meant auto_ptr.
     
    Dario Saccavino, Feb 4, 2008
    #2
    1. Advertising

  3. * Dario Saccavino:
    > It seems to me that operator->* is overloadable in C++ (just like
    > operator->), and it should be useful to implement in smart pointer
    > classes.
    >
    > However, I haven't found its implementation either in the standard
    > weak_ptr class, or in any boost smart pointer. Was it intentionally
    > left out of those headers?
    > Actually there's a workaround: just write
    > (ptr.operator->())->*memptr
    > instead of
    > ptr->*memptr
    > But it looks very ugly. How am I supposed to use member pointers with
    > smart pointers?


    In general it's not a good idea to use member pointers.

    They're tricky and yield unmaintainable code, they can circumvent access
    control, and mostly they indicate design errors. Regard member pointers
    as being in the same class of constructs as goto. Sometimes a necessary
    evil, but not something you'd employ without due consideration.

    If you absolutely must, try something like

    #include <iostream>
    #include <boost/shared_ptr.hpp>

    struct Foo
    {
    int value;
    Foo( int x = 42 ): value( x ) {};
    };

    template< typename Result, typename SmartPtr, typename Class >
    Result& operator->*( SmartPtr& p, Result Class::* pMember )
    {
    return (&*p)->*pMember; // Not guaranteed there is get().
    }

    int main()
    {
    boost::shared_ptr<Foo> p( new Foo );
    int Foo::* pFooInt = &Foo::value;

    p->*pFooInt = 43;
    std::cout << p->value << std::endl;
    }

    Note that to be general, the operator's result type must be adjusted to
    the union of the cv-qualification of the two arguments.

    And that's non-trivial.


    Cheers, & hth.,

    - Alf


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 4, 2008
    #3
  4. Dario Saccavino

    James Kanze Guest

    On Feb 4, 5:55 pm, "Alf P. Steinbach" <> wrote:
    > * Dario Saccavino:


    > > It seems to me that operator->* is overloadable in C++ (just like
    > > operator->), and it should be useful to implement in smart pointer
    > > classes.


    > > However, I haven't found its implementation either in the standard
    > > weak_ptr class, or in any boost smart pointer. Was it intentionally
    > > left out of those headers?
    > > Actually there's a workaround: just write
    > > (ptr.operator->())->*memptr
    > > instead of
    > > ptr->*memptr
    > > But it looks very ugly. How am I supposed to use member pointers with
    > > smart pointers?


    > In general it's not a good idea to use member pointers.


    Except when the alternatives are worse:).

    Seriously, I've used them a couple of times in the past, and
    almost every time, I've ended up going back, and reconverting
    the code to use delegates. Which requires a bit more
    boilerplate code, but is more flexible and more easily
    understood by other programmers.

    As for why std::auto_ptr (and the Boost smart pointers) don't
    support them, the main reason is probably because they're an
    open set. You'd need something like:
    template< typename U >
    ??? operator->*( U op ) ;
    And I'm not sure what you'd have to put in place of the ??? if U
    was a pointer to member function---the result of p->*pmf is a
    bound member function, and C++ doesn't have a type for it. (And
    of course, the very few times you might want to use pointers to
    members, it will be with a pointer to member function.)

    Anyway, given that it's neither useful nor implementable, I can
    sort of understand it not being present, even if it is an
    incompatibility with raw pointers.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan
    Replies:
    1
    Views:
    1,333
    Howard Hinnant
    Jun 23, 2003
  2. MotoK
    Replies:
    59
    Views:
    1,922
    Keith Thompson
    Sep 15, 2006
  3. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    500
    n2xssvv g02gfr12930
    Nov 27, 2005
  4. =?iso-8859-1?q?Ernesto_Basc=F3n?=

    delete operator on smart pointers.

    =?iso-8859-1?q?Ernesto_Basc=F3n?=, Jan 6, 2007, in forum: C++
    Replies:
    10
    Views:
    613
    mlimber
    Jan 13, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    733
Loading...

Share This Page