Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator ->* in smart pointers

Reply
Thread Tools

Operator ->* in smart pointers

 
 
Dario Saccavino
Guest
Posts: n/a
 
      02-04-2008
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
 
Reply With Quote
 
 
 
 
Dario Saccavino
Guest
Posts: n/a
 
      02-04-2008
On 4 Feb, 17:12, Dario Saccavino <(E-Mail Removed)> wrote:
>
> ...either in the standard weak_ptr class...


Typo. I clearly meant auto_ptr.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-04-2008
* 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?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-05-2008
On Feb 4, 5:55 pm, "Alf P. Steinbach" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
delete operator on smart pointers. =?iso-8859-1?q?Ernesto_Basc=F3n?= C++ 10 01-13-2007 01:25 AM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM



Advertisments