Article View: comp.std.c++
Article #663Re: Operator -> cannot return non-class types?
From: Brian McCauley
Date: Wed, 11 Nov 1992 18:43
Date: Wed, 11 Nov 1992 18:43
52 lines
2412 bytes
2412 bytes
In article <1992Nov11.092316.306@aut.abb.se>, dlarsson@aut.abb.se (Daniel Larsson) writes:
> To my surprise, I noticed that operator -> can only return the following
> three types:
> - a pointer to a class,
> - a class, or
> - a reference to a class.
>
> This means I cannot do simple things like:
>
> template< class T >
> class OffsetPtr
> {
> public:
> OffsetPtr();
> OffsetPtr( T* );
> ...
> T* operator ->();
> ...
> };
>
> OffsetPtr<char> ptrToString;
>
> What is the rationale behind this restriction? I couldn't find anything in
> C++PL 2nd ed. that mentions this restriction, although I didn't look too hard.
a->b
...where a is of a class type is interpreted as...
(a.operator->())->b
...which in turn may expand to...
((a.operator->()).operator->())->b
...and so on.
but in all cases the thing on the rhs of the -> is a class member. Since
types other that structs and classes do not have members it would be
useless for operator->() to return anything other than a pointer to a class or
a class for which operator->() is also defined.
Now what you could do is define operator T* (). Unfortuneately if you write
`a->b' where a is of a class without an operator->() the compiler does not
try compiling it as `(*a).b' so you'll have to write this out in full each
time. Likewise you can't write `a[5]' but you can write `*(a+5)'.
This has annoyed me in the past and I feel that the language definition
should employ user defined type conversions to resolve the lh operand of
the -> ->* and [] operators.
--
\\ ( ) NO BULLSHIT! from BAM (Brian McCauley)
. _\\__[oo
.__/ \\ /\@ E-mail: B.A.McCauley@bham.ac.uk
. l___\\ Fax: +44 21 625 2175
# ll l\\ Snail: 197 Harborne Lane, Birmingham, B29 6SS, UK
###LL LL\\ ICBM: 52.5N 1.9W
Technical Details
Message-ID:
<1992Nov11.184341.1@vax1.bham.ac.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!sun-barr!cs.utexas.edu!convex!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!pipex!warwick!bham!bhamvx!mccauleybaReferences:
<1992Nov11.092316.306@aut.abb.se>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++,comp.lang.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!cs.utexas.edu!convex!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!pipex!warwick!bham!bhamvx!mccauleyba From: mccauleyba@vax1.bham.ac.uk (Brian McCauley) Subject: Re: Operator -> cannot return non-class types? Sender: usenet@rs6000.bham.ac.uk (USENET News Service) Message-ID: <1992Nov11.184341.1@vax1.bham.ac.uk> Date: Wed, 11 Nov 1992 18:43:41 GMT Lines: 52 References: <1992Nov11.092316.306@aut.abb.se> Organization: University of Birmingham