C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about overloaded operator "*" of auto_ptr

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Zongjun Qi
Guest





PostPosted: Fri Sep 22, 2006 12:04 am    Post subject: Question about overloaded operator "*" of auto_ptr Reply with 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, 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





PostPosted: Fri Sep 22, 2006 5:10 am    Post subject: Re: Question about overloaded operator "*" of auto_ptr Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.