Tom Plunket wrote in news:e2d4409hgqe4s1jll2mvtucl4ogmvanu3l@4ax.com:
[color=blue]
> Nick Hounsome wrote:
>[color=green]
>> 1.
>> struct X
>> {
>> Y* operator->();
>> }
>>
>> 2. (the not obvious bit)
>> Because the compiler will reapply -> to the result.
>> This allows smart pointers, lazy construction, locking and much more.[/color]
>
> I've had some problems with this in the past.
>
> My compiler was Codewarrior, and the fix was to do
> (*myIterator)->MethodCall();
>
> This was with not-std:: containers, but is this basically telling
> me that I *should* be able to have a container of pointers to
> objects, and do iterator->call() and have it bounce through
> operator->() implementations 'til it gets to the end? Are there
> any gotchas that I may not have been aware of that would prevent
> this from happening correctly?
>[/color]
Yes, if you have:
#include <vector>
struct X
{
int x;
};
int main()
{
X x;
std::vector< X * > xpv( 10, &x );
std::vector< X * >::iterator ptr = xpv.begin();
}
Then ptr.operator -> () will return an X**, not an X*.
If you try ptr->x, the compiler will call ptr.operator -> () and
then try to apply the inbult (X**)->x, however there is no such
operator, inbult opertor -> *only* applies to pointers to struct's (*)
and X** is a pointer to a pointer not a pointer to a struct.
*) by 'struct' I mean aggragate i.e, struct, class or union.
So the Gotcha would be "there is no inbuilt operator -> for T**".
Rob.
--
http://www.victim-prime.dsl.pipex.com/