 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
maths_fan Guest
|
Posted: Sat Feb 28, 2004 12:07 pm Post subject: Overloading -> and ->* |
|
|
Can't understand how to overload these operations and for what reason?
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Sat Feb 28, 2004 3:04 pm Post subject: Re: Overloading -> and ->* |
|
|
"maths_fan" <maths_fan (AT) mail (DOT) ru> wrote:
| Quote: | Can't understand how to overload these operations and for what reason?
|
Can't understand your question real well either. Do you think you could
write it a little more clearly?
You overload these operators like any other operator. operator->() is
typically overloaded to implement smart pointers. A good example of a
reference-counting smart pointer, which demonstrates how to overload
operator->(), appears in the FAQ:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.21
Of course, I'm sure you consulted the FAQ before posting any questions here,
right?
Overloading the pointer to member operator (->*) is done a lot less
frequently, but again is used to implement smart pointers.
Best regards,
Tom
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Sat Feb 28, 2004 5:37 pm Post subject: Re: Overloading -> and ->* |
|
|
"maths_fan" <maths_fan (AT) mail (DOT) ru> wrote
| Quote: | Can't understand how to overload these operations and for what reason?
|
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.
2a. I've never come across a reason to overload ->*
|
|
| Back to top |
|
 |
Tom Plunket Guest
|
Posted: Sun Feb 29, 2004 6:52 pm Post subject: Re: Overloading -> and ->* |
|
|
Nick Hounsome wrote:
| Quote: | 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.
|
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?
thx,
-tom!
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Sun Feb 29, 2004 8:27 pm Post subject: Re: Overloading -> and ->* |
|
|
Tom Plunket wrote in news:e2d4409hgqe4s1jll2mvtucl4ogmvanu3l (AT) 4ax (DOT) com:
| Quote: | Nick Hounsome wrote:
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.
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?
|
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/
|
|
| Back to top |
|
 |
Tom Plunket Guest
|
Posted: Sun Feb 29, 2004 8:47 pm Post subject: Re: Overloading -> and ->* |
|
|
Rob Williscroft wrote:
| Quote: | 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**".
|
So this is to say if I had:
std::vector< boost::shared_ptr::iterator it;
it->x = 3;
would compile properly?
I think I get it. I was reading previous information on this
topic to suggest that there *was* a built-in operator-> that was
triggered that would "do what you expect," although now that I
think about it it seems obviously wrong since you wouldn't (?)
want:
X** x;
x->x = 5;
to compile.
thanks-
-tom!
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Sun Feb 29, 2004 9:13 pm Post subject: Re: Overloading -> and ->* |
|
|
Tom Plunket wrote in news:mpj440tgnbje77gti7nhc44oa3gm3rn58b (AT) 4ax (DOT) com:
| Quote: | Rob Williscroft wrote:
[snip]
So the Gotcha would be "there is no inbuilt operator -> for T**".
So this is to say if I had:
std::vector< boost::shared_ptr::iterator it;
it->x = 3;
would compile properly?
|
Alas it doesen't, it.operator ->() returns a boost::shared_ptr<X> *.
it->x would work if the return value was shared_ptr<X> &, but
then std::vector's of simple structs wouldn't work.
Also you couldn't do: it->unique() (*).
*) For those that don't know, unique() is a member of shared_ptr<>:
http://www.boost.org/libs/smart_ptr/shared_ptr.htm.
| Quote: | I think I get it. I was reading previous information on this
topic to suggest that there *was* a built-in operator-> that was
triggered that would "do what you expect," although now that I
think about it it seems obviously wrong since you wouldn't (?)
want:
X** x;
x->x = 5;
to compile.
|
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Tom Plunket Guest
|
Posted: Mon Mar 01, 2004 5:52 am Post subject: Re: Overloading -> and ->* |
|
|
Rob Williscroft wrote:
| Quote: | So this is to say if I had:
std::vector< boost::shared_ptr::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?
-tom!
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Mon Mar 01, 2004 6:44 am Post subject: Re: Overloading -> and ->* |
|
|
Tom Plunket wrote in news:sqj5405gh69d4p18lv3r1t6tc7bnvbci9m (AT) 4ax (DOT) com:
| Quote: | Rob Williscroft wrote:
So this is to say if I had:
std::vector< boost::shared_ptr::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/
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|