473,545 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading -> and ->*

Can't understand how to overload these operations and for what reason?
Jul 22 '05 #1
8 1756

"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
Jul 22 '05 #2

"maths_fan" <ma*******@mail .ru> wrote in message
news:c1******** *************** **@posting.goog le.com...
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 ->*
Jul 22 '05 #3
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!
Jul 22 '05 #4
Tom Plunket wrote in news:e2******** *************** *********@4ax.c om:
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/
Jul 22 '05 #5
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_p tr<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!
Jul 22 '05 #6
Tom Plunket wrote in news:mp******** *************** *********@4ax.c om:
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_p tr<X> >::iterator it;
it->x = 3;

would compile properly?


Alas it doesen't, it.operator ->() returns a boost::shared_p tr<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/
Jul 22 '05 #7
Rob Williscroft wrote:
So this is to say if I had:

std::vector< boost::shared_p tr<X> >::iterator it;
it->x = 3;

would compile properly?


Alas it doesen't, it.operator ->() returns a boost::shared_p tr<X> *.


Oh...

I fail to see now, then, where this operator-> does this bouncing
then... When does it, when can it, come up?
-tom!
Jul 22 '05 #8
Tom Plunket wrote in news:sq******** *************** *********@4ax.c om:
Rob Williscroft wrote:
> So this is to say if I had:
>
> std::vector< boost::shared_p tr<X> >::iterator it;
> it->x = 3;
>
> would compile properly?
>


Alas it doesen't, it.operator ->() returns a boost::shared_p tr<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/
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2421
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 SLinkList.cxx:3, from list.cxx:1: SLinkList.h:20: ISO C++ forbids declaration of `type name' with no
3
1489
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 of specification, as friends in the class that will use them? Thanks, TJ
2
2432
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. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include...
4
2469
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 templatized operator>>() as a member that can deal with the built in C types (int, char, float...) template <class tType> STR& STR::operator>>(tType &...
6
1913
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
0
1097
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 used . Is there a way out for const member function ?
6
1779
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:
5
2638
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 here And this came out, via prettify: <addresssnippet siteurl="http%3A//apartmentsapart.com"...
2
4411
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 & operator>(std::istream & in, const Complex & a); // the methods correspond to the friend std::istream & operator>(std::istream & in, const Complex & a) {...
3
1429
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 is reserved, the "" is used for lists.
0
7457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1869
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.