home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pascal / 6678 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  5.4 KB

  1. Xref: sparky comp.lang.pascal:6678 comp.object:4254
  2. Newsgroups: comp.lang.pascal,comp.object
  3. Path: sparky!uunet!mcsun!news.funet.fi!network.jyu.fi!sakkinen
  4. From: sakkinen@jyu.fi (Markku Sakkinen)
  5. Subject: Re: BP/TP OOP is missing something...
  6. Message-ID: <1992Nov18.104536.9985@jyu.fi>
  7. Organization: University of Jyvaskyla, Finland
  8. References: <dmurdoch.282.722020256@mast.queensu.ca> <10773@vice.ICO.TEK.COM> <dmurdoch.171.722054313@mast.queensu.ca>
  9. Date: Wed, 18 Nov 1992 10:45:36 GMT
  10. Lines: 91
  11.  
  12. (This discussion comes from comp.lang.pascal.   I thought cross-posting
  13. to comp.object might be appropriate at this point.)
  14.  
  15. In article <dmurdoch.171.722054313@mast.queensu.ca> dmurdoch@mast.queensu.ca (Duncan Murdoch) writes:
  16. >In article <10773@vice.ICO.TEK.COM> bobbe@vice.ICO.TEK.COM (Robert Beauchaine) writes:
  17. >> ...
  18. >No, I think there's a difference between allocating variables and 
  19. >initializing them.  The compiler is perfectly capable of automatic 
  20. >allocation of objects when they're local variables, and it'll automatically 
  21. >free up the storage when it goes out of scope.
  22. >
  23. >However, calling a constructor for an object is akin to initializing it.  
  24. >As far as I can recall, there aren't any cases in Pascal where variables 
  25. >are initialized without explicit action by the programmer. [...]
  26. > ...
  27. >On the other hand, it would be a far reaching change that would 
  28. >affect the language in lots of little ways.  Right now, declarations 
  29. >are declarations, they aren't executable statements.  You're suggesting 
  30. >that the distinction be blurred.  
  31.  
  32. In a way, "you cannot eat your cake and have it too".
  33. If one wants to do fully-fledged OOP, one cannot fully conserve all of
  34. Pascal's original principles.  Most importantly, OOP implies information
  35. hiding and "decentralised design":  you must accept that other classes
  36. are doing things behind your back (or behind the scenes), and you don't care.
  37. Some initialisation and (less often) finalisation needs can really be
  38. an internal affair of a class, about which its clients should not bother.
  39.  
  40. There is at least one spot in standard Pascal where automatic initialisation
  41. happens: file variables (admittedly a murky part of Pascal semantics).
  42. After _only_ declaring a file variable, it is also initialised to
  43. a well-defined state such that you cannot do any other operations
  44. before calling 'reset' or 'rewrite'.
  45. I would guess that good Pascal implementation may also initialise
  46. pointer variables to NIL so that using them before assignment
  47. will not cause catastrophic effects.
  48.  
  49. Class objects are often similar to file variables in that allocating
  50. raw memory is not sufficient for applying ordinary operations immediately.
  51. Perhaps some fields of each object must be given particular initial
  52. values in order to establish the class invariant on which all other
  53. operations except the constructor(s) depend.
  54.  
  55. Often, a variable-sized chunk of dynamic memory must be allocated
  56. "behind the scenes" during initialisation (or at other times).
  57. Such cases are the most common cause of the need for finalisation:
  58. since neither Pascal nor C++ support garbage collection,
  59. that memory must also be released.
  60.  
  61. >>  This is exactly why I like the C++ approach.  With automated
  62. >>  construction/destruction, operator overloading, etc, objects are
  63. >>  much more seamlessly integrated into an application as extensions
  64. >>  of the basic data types.  Once the class is properly defined, the
  65. >>  programmer never has to consider the objects as anything except
  66. >>  predefined atomic stuctures again.
  67. >
  68. >On the other hand, it seems that it would be rather inefficient in lots of 
  69. >code.  For example, I often write routines that need to do calculations 
  70. >before they can call the constructor for an object; if it had already been 
  71. >constructed, would I have to destroy it before changing it?  A simple 
  72. >example would be reading a matrix from a file:  I might read a line to get 
  73. >the matrix dimensions, construct an appropriate matrix, and then read the 
  74. >data into it.  How are situations like that handled with automatic 
  75. >constructors?  Or is the automatic construction optional?  If so, that just 
  76. >leads to code equivalent to the TP code.
  77.  
  78. The idea is that all other uses of an object happen between its
  79. initialisation and the finalisation;  of course you cannot change
  80. an object after it has been destroyed.  Your matrix example would
  81. be much easier to do in Simula or C++ than Pascal, because they allow
  82. variables to be declared even within blocks (not only in function and
  83. procedure headers), and they allow also initialisation parameters.
  84. In any flavour of OO Pascal without such parameters, the constructor
  85. of a general matrix class (i.e. without fixed dimensions) would most
  86. naturally create only a "handle" for an actual matrix.
  87.  
  88. Of course, automatic construction is optional (e.g. in C++):
  89. the class designer is not required to define any constructors or destructors
  90. if he/she does not perceive real need for them.
  91. (Well, in C++ you sometimes had better write a do-nothing virtual destructor
  92. to be safe, but that is a special quirk of that language.)
  93.  
  94. ----------------------------------------------------------------------
  95. Markku Sakkinen (sakkinen@jytko.jyu.fi)
  96.        SAKKINEN@FINJYU.bitnet (alternative network address)
  97. Department of Computer Science and Information Systems
  98. University of Jyvaskyla (a's with umlauts)
  99. PL 35
  100. SF-40351 Jyvaskyla (umlauts again)
  101. Finland
  102. ----------------------------------------------------------------------
  103.