home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1543 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  2.3 KB

  1. Xref: sparky comp.std.c++:1543 comp.lang.c++:16117
  2. Newsgroups: comp.std.c++,comp.lang.c++
  3. Path: sparky!uunet!sun-barr!cs.utexas.edu!convex!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!pipex!warwick!bham!bhamvx!mccauleyba
  4. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  5. Subject: Re: Operator -> cannot return non-class types?
  6. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  7. Message-ID: <1992Nov11.184341.1@vax1.bham.ac.uk>
  8. Date: Wed, 11 Nov 1992 18:43:41 GMT
  9. Lines: 52
  10. References: <1992Nov11.092316.306@aut.abb.se>
  11. Organization: University of Birmingham
  12.  
  13. In article <1992Nov11.092316.306@aut.abb.se>, dlarsson@aut.abb.se (Daniel Larsson) writes:
  14. > To my surprise, I noticed that operator -> can only return the following
  15. > three types:
  16. >    - a pointer to a class,
  17. >    - a class, or
  18. >    - a reference to a class.
  19. > This means I cannot do simple things like:
  20. >   template< class T >
  21. >   class OffsetPtr 
  22. >     {
  23. >     public:
  24. >       OffsetPtr();
  25. >       OffsetPtr( T* );
  26. >       ...
  27. >       T* operator ->();
  28. >       ...
  29. >     };
  30. >   OffsetPtr<char> ptrToString;
  31. > What is the rationale behind this restriction? I couldn't find anything in
  32. > C++PL 2nd ed. that mentions this restriction, although I didn't look too hard.
  33.  
  34. a->b
  35.   ...where a is of a class type is interpreted as...
  36. (a.operator->())->b
  37.   ...which in turn may expand to...
  38. ((a.operator->()).operator->())->b
  39.   ...and so on.
  40.  
  41. but in all cases the thing on the rhs of the -> is a class member. Since
  42. types other that structs and classes do not have members it would be
  43. useless for operator->() to return anything other than a pointer to a class or
  44. a class for which operator->() is also defined.
  45.  
  46. Now what you could do is define operator T* (). Unfortuneately if you write
  47. `a->b' where a is of a class without an operator->() the compiler does not
  48. try compiling it as `(*a).b' so you'll have to write this out in full each
  49. time. Likewise you can't write `a[5]' but you can write `*(a+5)'.
  50.  
  51. This has annoyed me in the past and I feel that the language definition
  52. should employ user defined type conversions to resolve the lh operand of
  53. the -> ->* and [] operators.
  54. -- 
  55.     \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)
  56.  .  _\\__[oo 
  57. .__/  \\ /\@  E-mail: B.A.McCauley@bham.ac.uk
  58. .  l___\\        Fax: +44 21 625 2175
  59.  # ll  l\\     Snail: 197 Harborne Lane, Birmingham, B29 6SS, UK
  60. ###LL  LL\\     ICBM: 52.5N 1.9W
  61.