Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Operator ->* in smart pointers (http://www.velocityreviews.com/forums/t589436-operator-in-smart-pointers.html)

Dario Saccavino 02-04-2008 04:12 PM

Operator ->* in smart pointers
 
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 02-04-2008 04:16 PM

Re: Operator ->* in smart pointers
 
On 4 Feb, 17:12, Dario Saccavino <kath...@gmail.com> wrote:
>
> ...either in the standard weak_ptr class...


Typo. I clearly meant auto_ptr.

Alf P. Steinbach 02-04-2008 04:55 PM

Re: Operator ->* in smart pointers
 
* 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?

James Kanze 02-05-2008 09:53 AM

Re: Operator ->* in smart pointers
 
On Feb 4, 5:55 pm, "Alf P. Steinbach" <al...@start.no> 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:james.kanze@gmail.com
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


All times are GMT. The time now is 04:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.