 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Zongjun Qi Guest
|
Posted: Fri Sep 22, 2006 12:04 am Post subject: Question about overloaded operator "*" of auto_ptr |
|
|
In auto_ptr declaration, we can find auto_ptr overloaded the
"dereference" operator "*". Below is its signature:
T& operator*() {return *ptr;}
But my question is: since overloaded operators only expect to work with
a LEFT OPERAND of a instance of the class, then how can we use it in
our code like this:
auto_ptr<int> p<new int>;
....
*p = 8;
Instead, according to the overloaded operator usage syntax, we should
use it like this
p* = 8.
Well, aparantly "*p = 8" is the working way, after I tested on my linux
system. But why? ...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Fri Sep 22, 2006 5:10 am Post subject: Re: Question about overloaded operator "*" of auto_ptr |
|
|
Zongjun Qi wrote:
| Quote: | In auto_ptr declaration, we can find auto_ptr overloaded the
"dereference" operator "*". Below is its signature:
T& operator*() {return *ptr;}
But my question is: since overloaded operators only expect to work with
a LEFT OPERAND of a instance of the class [...]
Instead, according to the overloaded operator usage syntax, we should
use it like this
p* = 8. [...]
|
Seems like you have to hit the books a little -- go back to
your favourite C++ book's chapter on operator overloading, and
you should immediately understand why, after reading about the
rules of operator overloading (possibly in a section "Rules and
Limitations about operator overloading" in that chapter). (you
could also try a Google search for online tutorials -- I'm sure
you'llfind many)
Also, ask yourself the following: is that operator* a unary or
a binary operator? What about operator overloading for each of
these categories?
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| 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
|
|