home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11509 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.1 KB  |  88 lines

  1. Xref: sparky comp.lang.c++:11509 comp.std.c++:950
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  4. From: jss@lucid.com (Jerry Schwarz)
  5. Subject: Re: run-time type checking (was: Re: Covariant Types in Derived Classes)
  6. Message-ID: <1992Jul25.022147.24152@lucid.com>
  7. Sender: usenet@lucid.com
  8. Reply-To: jss@lucid.com (Jerry Schwarz)
  9. Organization: Lucid, Inc.
  10. References: <1992Jul22.022218.1115@cadsun.corp.mot.com> <1992Jul23.154254.5306@ucc.su.OZ.AU> <1992Jul24.143359.3602@advtech.uswest.com> <1992Jul24.192207.15267@ucc.su.OZ.AU>
  11. Date: Sat, 25 Jul 92 02:21:47 GMT
  12. Lines: 74
  13.  
  14. In article <1992Jul24.192207.15267@ucc.su.OZ.AU>, maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. |> 
  16. |>     But that wont PROVE anything. You have to give me
  17. |> an example of a system that uses downcasting and then I'll
  18. |> show how to write it without downcasting.
  19. |> 
  20. |>     Please restrict examples to domestic systems, since
  21. |> I already agree all bets are off for foreign systems and
  22. |> also for introspective ones like, say, debuggers, OODBMS,
  23. |> etc.
  24. |> 
  25. |>     Lets agree for example to restrict ourselves to
  26. |> text input and output and no disk storage. Lets agree
  27. |> that the program must handle a fixed number of types.
  28. |> And that it reads text in, does some calculations,
  29. |> and then prints the answers.
  30. |> 
  31. |>     Such a program creates ALL the objects, manipulates
  32. |> them, and then it terminates.
  33. |> 
  34.  
  35. Here is a typical example of my use of downcasting. Downcasting
  36. is not essential in this example, but I think it makes the design
  37. cleaner.
  38.  
  39. I want to define an "indent stream" derived from ostream.
  40. An indent stream contains an indentation level.  
  41.  
  42. class indentstream : public ostream {
  43.    public:
  44.     int level ;  // Public only to keep example short.
  45.     indentstream(streambuf* sb) : ostream(sb), level(0) { }
  46. } ;
  47.  
  48. The level is used by a manipulator.
  49.  
  50. ostream& newline(ostream& os) {
  51.     indentstream* is = CAST<indentstream*>os ;  // notation suggestive
  52.     os.put(\n) ;
  53.     if ( is ) {
  54.         for ( int n = 0 ; n < is->level ; ++n ) is.put('\t') ;
  55.     }
  56.     os.flush() ;
  57. }
  58.  
  59. Typical uses look like
  60.  
  61.      indentstream myout(...) ;
  62.      ...
  63.      myout.level++ ;
  64.      // x and y inserters do not have to understand indentation as long
  65.      // as they put everything on a single line.
  66.      myout << x << newline << y << newline ;
  67.      myout.level--
  68.  
  69. This class isn't perfect, but I hope you get the idea.  More elaborate
  70. methods can be devised that put the newline detection in the
  71. streambuf and avoid newline.  These also require downcasting so
  72. that you can be sure the streambuf is the right type before you tell
  73. it to change level.
  74.  
  75. |> >
  76. |> >>    Inother words, in a *domestic* system, any apparent 
  77. |> >>need for downcasting should be used as an indicator of flawed design.
  78. |> >>
  79.  
  80. indentstream can be done without derivation and without downcasting by use
  81. of the iword facility of streams, but that feature of streams is generally
  82. regarded as baroque and unappealing.  It was included in the stream
  83. interface precisely because safe downcasting wasn't available.  
  84. People frequently just go ahead and use an unsafe cast in "newline"
  85. (or its equivalent).  
  86.  
  87.    -- Jerry Schwarz
  88.