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

  1. Xref: sparky comp.std.c++:1559 comp.lang.c++:16227
  2. Newsgroups: comp.std.c++,comp.lang.c++
  3. Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
  4. From: kanze@us-es.sel.de (James Kanze)
  5. Subject: Re: Operator -> cannot return non-class types?
  6. In-Reply-To: mccauleyba@vax1.bham.ac.uk's message of Wed, 11 Nov 1992 18:43:41 GMT
  7. Message-ID: <KANZE.92Nov13191617@slsvhat.us-es.sel.de>
  8. Sender: news@us-es.sel.de
  9. Organization: SEL
  10. References: <1992Nov11.092316.306@aut.abb.se> <1992Nov11.184341.1@vax1.bham.ac.uk>
  11. Date: 13 Nov 92 19:16:17
  12. Lines: 72
  13.  
  14. In article <1992Nov11.092316.306@aut.abb.se>, dlarsson@aut.abb.se
  15. (Daniel Larsson) writes:
  16.  
  17. |> > To my surprise, I noticed that operator -> can only return the following
  18. |> > three types:
  19. |> >    - a pointer to a class,
  20. |> >    - a class, or
  21. |> >    - a reference to a class.
  22. |> > 
  23. |> > This means I cannot do simple things like:
  24. |> > 
  25. |> >   template< class T >
  26. |> >   class OffsetPtr 
  27. |> >     {
  28. |> >     public:
  29. |> >       OffsetPtr();
  30. |> >       OffsetPtr( T* );
  31. |> >       ...
  32. |> >       T* operator ->();
  33. |> >       ...
  34. |> >     };
  35. |> > 
  36. |> >   OffsetPtr<char> ptrToString;
  37. |> > 
  38. |> > What is the rationale behind this restriction? I couldn't find anything in
  39. |> > C++PL 2nd ed. that mentions this restriction, although I didn't look too hard.
  40.  
  41. |> a->b
  42. |>   ...where a is of a class type is interpreted as...
  43. |> (a.operator->())->b
  44. |>   ...which in turn may expand to...
  45. |> ((a.operator->()).operator->())->b
  46. |>   ...and so on.
  47.  
  48. |> but in all cases the thing on the rhs of the -> is a class member.
  49. |> Since types other that structs and classes do not have members it
  50. |> would be useless for operator->() to return anything other than a
  51. |> pointer to a class or a class for which operator->() is also defined.
  52.  
  53. |> Now what you could do is define operator T* (). Unfortuneately if you
  54. |> write `a->b' where a is of a class without an operator->() the
  55. |> compiler does not try compiling it as `(*a).b' so you'll have to write
  56. |> this out in full each time. Likewise you can't write `a[5]' but you
  57. |> can write `*(a+5)'.
  58.  
  59. |> This has annoyed me in the past and I feel that the language
  60. |> definition should employ user defined type conversions to resolve the
  61. |> lh operand of the -> ->* and [] operators.
  62.  
  63. IMHO, this should only be a problem in templates.
  64.  
  65. Basically, I don't offhand see anything in the language definition
  66. that would restrict an operator->() (the function) from returning
  67. whatever it wishes.  On the other hand, there is no way to use this
  68. operator *unless* it returns a pointer to a class.  This is no doubt
  69. why your compiler introduced the restriction.
  70.  
  71. Normally, of course, you would never want to write a function that
  72. could not be called.  However, in the case of templates, it is
  73. possible that you might, since it could be called for the
  74. instantiations using a class type, and any attempt to use it for the
  75. instantiations using a non-class type would cause a compiler error at
  76. the point of use.
  77.  
  78. (Actually, you can call the function, even for a non-class type
  79. result, explicitly: "c = operator->( p ) ;", for example.  However, I
  80. don't think that this is really the idea behind operator overloading.)
  81. --
  82. James Kanze            GABI Software, Sarl.
  83. email: kanze@us-es.sel.de    8 rue du Faisan
  84.                 67000 Strasbourg
  85.                 France
  86.