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 

overloading subscription operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
bipod.rafique@gmail.com
Guest





PostPosted: Tue Sep 06, 2005 5:25 am    Post subject: overloading subscription operator Reply with quote



Hello All,

I am sorry if I am posting this topic in the wrong group. Do let me
know where to post if that's the case.

My question is on operator overloading. The standard (supposedly) says
that we can't overload assignment (=) operator as a non-member
function. The reason I figured is due to the fact that criminals
(kidding!) may overload the assignment operator as

operator(basic_type, user_type);

allowing one to use

2 = a;

where a is an object of user defined class.

Is this true? Or is there more to it?

Given that it is true the standard also forbid overloading subscription
operator ([]) as a non-member function. Casting into the same
reasoning, I thought, this would stop one to do

2[a]

where a is an object of user defined class.

But but but, I was told 2[a] is legal! if a is a pointer! so why can't
we allow 2[a] to be legal if a is an object? Perhaps the answer simply
is "it does not make sense!" or is there more?

Thanks and I am sorry I have bothered you all...

Bipod...

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
kanze
Guest





PostPosted: Tue Sep 06, 2005 2:50 pm    Post subject: Re: overloading subscription operator Reply with quote



[email]bipod.rafique (AT) gmail (DOT) com[/email] wrote:

Quote:
I am sorry if I am posting this topic in the wrong group. Do
let me know where to post if that's the case.

My question is on operator overloading. The standard
(supposedly) says that we can't overload assignment (=)
operator as a non-member function. The reason I figured is due
to the fact that criminals (kidding!) may overload the
assignment operator as

operator(basic_type, user_type);

allowing one to use

2 = a;

where a is an object of user defined class.

Is this true? Or is there more to it?

I think that the real reason is because the compiler may
generate an operator= of its own. It's important for a user
defined operator= to be a member so that the compiler can know
whether it has to generate one or not.

Quote:
Given that it is true the standard also forbid overloading
subscription operator ([]) as a non-member function.

To tell the truth, I don't know what the reason was. I can't
say that the restriction has ever bothered me, though.

Quote:
Casting into the same reasoning, I thought, this would stop
one to do

2[a]

where a is an object of user defined class.

But but but, I was told 2[a] is legal! if a is a pointer!

True. Useful for code for IOCCC, but not for much of anything
else.

Quote:
so why can't we allow 2[a] to be legal if a is an object?
Perhaps the answer simply is "it does not make sense!" or is
there more?

Maybe supporting IOCCC type code wasn't a primary design goal of
C++?

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards 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.