Can't understand how to overload these operations and for what reason? 8 1732
"maths_fan" <ma*******@mail.ru> wrote: 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-lit...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
"maths_fan" <ma*******@mail.ru> wrote in message
news:c1*************************@posting.google.co m... 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 ->*
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?
thx,
-tom!
Tom Plunket wrote in news:e2********************************@4ax.com: 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/
Rob Williscroft wrote: 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<X> >::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!
Tom Plunket wrote in news:mp********************************@4ax.com: 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<X> >::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.
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/
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?
-tom!
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Yu Lianqing |
last post by:
Hi, all
I am writing an overloading operator >> function for a template class
and can't make it right. G++ 3.2 (Redhat8.0) gives the following
errors:
g++ -c list.cxx
In file included from...
|
by: TJ |
last post by:
Hi,
I've been referring to many codes and books and always see that the
stream insertion operators are overloaded as friends. Why is that?
Are there any other overloading that need the same type...
|
by: franklini |
last post by:
hello people i. can anybody help me, i dont know what is wrong with
this class. it has something to do with the me trying to override the
input output stream. if i dont override it, it works fine....
|
by: hall |
last post by:
Hi all.
I have run into a problem of overloading a templatized operator>> by a
specialized version of it. In short (complete code below), I have
written a stream class, STR, which defines a...
|
by: n2xssvv g02gfr12930 |
last post by:
Does anyone know of an example of overloading operator ->*
Cheers
JB
|
by: rama270 |
last post by:
Problem:
The operator -> is overloaded thus
Class * operator-> () { return this; }
but this causes a problem for const member functions . The purpose of overloading is that both . and -> can be...
|
by: iLL |
last post by:
Okay, I’m just a little confused on exactly what the system is doing when I say:
#include <iostream>
class test
{
private:
int i;
public:
|
by: John Nagle |
last post by:
This, which is from a real web site, went into BeautifulSoup:
<param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer
fantastic rates for selected weeks or days!!&blinkt=Click...
|
by: Colonel |
last post by:
It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.
// the declaration
friend std::istream &...
|
by: mosi |
last post by:
Python matrices are usually defined with numpy scipy array or similar.
e.g.
I would like to have easier way of defining matrices,
for example:
Any ideas how could this be done? The ";" sign...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |