home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11543 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  4.0 KB

  1. Xref: sparky comp.lang.c++:11543 comp.std.c++:957
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!darwin.sura.net!mips!mips!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  4. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  5. Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
  6. Message-ID: <1992Jul25.174252.25315@ucc.su.OZ.AU>
  7. Sender: news@ucc.su.OZ.AU
  8. Nntp-Posting-Host: extro.ucc.su.oz.au
  9. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  10. References: <1992Jul24.143359.3602@advtech.uswest.com> <1992Jul24.192207.15267@ucc.su.OZ.AU> <1992Jul25.022147.24152@lucid.com>
  11. Date: Sat, 25 Jul 1992 17:42:52 GMT
  12. Lines: 99
  13.  
  14. In article <1992Jul25.022147.24152@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  15. >
  16. >Here is a typical example of my use of downcasting. Downcasting
  17. >is not essential in this example, but I think it makes the design
  18. >cleaner.
  19.  
  20.     Perhaps:
  21.     a) stream design is flawed (I happen to think this anyway)
  22.     b) where you use the downcast something else would be
  23.     even better.
  24. >
  25. >I want to define an "indent stream" derived from ostream.
  26. >An indent stream contains an indentation level.  
  27.  
  28.     You would pick streams, which I never use :-)
  29.     But the problem seems to be well specified :-)
  30. >
  31. >class indentstream : public ostream {
  32. >   public:
  33. >    int level ;  // Public only to keep example short.
  34. >    indentstream(streambuf* sb) : ostream(sb), level(0) { }
  35. >} ;
  36. >
  37. >The level is used by a manipulator.
  38. >
  39. >ostream& newline(ostream& os) {
  40. >    indentstream* is = CAST<indentstream*>os ;  // notation suggestive
  41. >    os.put(\n) ;
  42. >    if ( is ) {
  43. >        for ( int n = 0 ; n < is->level ; ++n ) is.put('\t') ;
  44. >    }
  45. >    os.flush() ;
  46. >}
  47. >
  48. >Typical uses look like
  49. >
  50. >     indentstream myout(...) ;
  51. >     ...
  52. >     myout.level++ ;
  53. >     // x and y inserters do not have to understand indentation as long
  54. >     // as they put everything on a single line.
  55. >     myout << x << newline << y << newline ;
  56. >     myout.level--
  57. >
  58. >This class isn't perfect, but I hope you get the idea.  More elaborate
  59. >methods can be devised that put the newline detection in the
  60. >streambuf and avoid newline.  These also require downcasting so
  61. >that you can be sure the streambuf is the right type before you tell
  62. >it to change level.
  63.  
  64.     I cant comment for sure on this, I dont have enough experience
  65. with streams. 
  66.  
  67.     Perhaps streams have to regarded as a foreign interface,
  68. in which case downcasting might be ok.
  69.  
  70.     Perhaps streams are badly designed (the flaw is in them, not
  71.     your code).
  72.  
  73.     And perhaps the fact that you are using a downcast indicates
  74.     that you are cheating the system:
  75.  
  76. I get one of your streams (but I only know it is an ordinary stream).
  77. I write out a file, then read it back in and ---oh, where
  78. did those tabs come from---there's a BUG in the stream package!
  79.  
  80. Mm, is there, I say, and look at the class semantics
  81. (I read the source code). No, I say, there is NO WAY that
  82. this can happen, there is NO virtual function that would allow
  83. that freedom.
  84.  
  85. You see this is an example then of how you could do ANYTHING to
  86. something I thought was an implementation fo a stream,
  87. you have invalidated the semantics by downcasting.
  88.  
  89. >indentstream can be done without derivation and without downcasting by use
  90. >of the iword facility of streams, but that feature of streams is generally
  91. >regarded as baroque and unappealing.  
  92.  
  93.     And I checked this out and found iword was NOT being fiddled!
  94.  
  95.  
  96. >It was included in the stream
  97. >interface precisely because safe downcasting wasn't available.  
  98.  
  99.     Perhaps it was provided because ANY casting is bad news :-)
  100.  
  101. >People frequently just go ahead and use an unsafe cast in "newline"
  102. >(or its equivalent).  
  103. >   -- Jerry Schwarz
  104.  
  105. Your example may be a valid use of downcasting, I'm sorry that
  106. I just dont know enough about streams to argue it.
  107.  
  108. -- 
  109. ;----------------------------------------------------------------------
  110.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  111.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  112. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  113.