Tom Plunket wrote in news:sq********************************@4ax.com:
Rob Williscroft wrote:
> So this is to say if I had:
>
> std::vector< boost::shared_ptr<X> >::iterator it;
> it->x = 3;
>
> would compile properly?
>
Alas it doesen't, it.operator ->() returns a boost::shared_ptr<X> *.
Oh...
I fail to see now, then, where this operator-> does this bouncing
then... When does it, when can it, come up?
#include <iostream>
#include <ostream>
using std::cerr;
//Simple case
struct X
{
int data;
};
struct Y
{
X *xp;
X *operator -> () { return xp; }
};
void simple_example()
{
X x = { 0 };
Y y = { &x };
y->data = 1;
cerr << x.data << '\n';
}
/* y->data calls y.operator ->() which returns X*,
compiler uses inbuilt operator ->.
*/
//Complex case:
struct Z
{
Y array[2];
bool b;
/* Note: operator -> () returns a reference not a pointer
*/
Y & operator -> () { return array[b]; }
};
void complex_example()
{
X x1 = { 0 }, x2 = { 0 };
Z z = { { &x1, &x2 }, false };
z->data = 1;
z.b = true;
z->data = 2;
cerr << x1.data << '\n';
cerr << x2.data << '\n';
}
/* z->data calls x.operator -> () which returns Y &,
compiler calls Y::operator -> (), which returns X *,
compiler uses inbuilt operator ->.
*/
int main()
{
simple_example();
complex_example();
}
In short bouncing as you call it continues until a user defined
operator -> () returns a pointer, and then the language's inbuilt
rules are applied.
Rob.
--
http://www.victim-prime.dsl.pipex.com/