Topic: overloading subscription operator
Author: bipod.rafique@gmail.com
Date: Mon, 5 Sep 2005 23:25:31 CST Raw View
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 ]
Author: "kanze" <kanze@gabi-soft.fr>
Date: Tue, 6 Sep 2005 08:50:13 CST Raw View
bipod.rafique@gmail.com wrote:
> 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.
> 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.
> 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.
> 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 ]