home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / amateur / 428 < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.9 KB  |  71 lines

  1. Xref: sparky alt.amateur-comp:428 comp.lang.c:19096 comp.lang.c++:18614 comp.misc:4734
  2. Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  3. From: torek@horse.ee.lbl.gov (Chris Torek)
  4. Newsgroups: alt.amateur-comp,comp.lang.c,comp.lang.c++,comp.misc
  5. Subject: Re: What is Object Oriented Programming? Is C doomed?
  6. Date: 31 Dec 1992 22:59:48 GMT
  7. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  8. Lines: 58
  9. Message-ID: <28183@dog.ee.lbl.gov>
  10. References: <1fs9ufINN4ch@usenet.INS.CWRU.Edu> <Bzo97n.DHq@vcd.hp.com>
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <Bzo97n.DHq@vcd.hp.com> tonyl@vcd.hp.com (Tony Long) writes:
  14. >    Classic C has no "template" for an abstract structure--the 'C'
  15. >structures can only contain the data elements themselves and have no direct
  16. >relationship to functions which operate upon them.
  17.  
  18. I believe someone else has already pointed this out, but just to be
  19. absolutely sure, this is true in one sense but false in another.  Since
  20. C has pointer to functions, one can embed such pointers into data
  21. objects.  These correspond quite closely to C++ virtual functions
  22. (incidentally, all C++ class functions can always be implemented as
  23. virtual functions, although there may be some cost at runtime to do
  24. so).
  25.  
  26. An example will probably make this clear.  In C++, one might write
  27. something like:
  28.  
  29.     class foo {
  30.         int    data;
  31.         virtual void function();
  32.     };
  33.  
  34. (we will ignore the fact that no one would write quite like this :-) ).
  35. This can be translated directly into C as:
  36.  
  37.     struct foo {
  38.         int    data;
  39.         void    (*function)(struct foo *);
  40.     };
  41.  
  42. Of course, given a foo object `object', in C++ one merely writes:
  43.  
  44.     object.function();
  45.  
  46. where in C one has to write:
  47.  
  48.     object.function(&object);
  49.  
  50. C also lacks constructors and destructors, which becomes more important
  51. when dealing with static and automatic objects rather than pointers and
  52. allocation and free functions; and C++ has finer grained access control
  53. (private, public, and friends) than does C (all or nothing).  In
  54. effect, C++ automates a number of niggling details that must be written
  55. out explicitly in C.  By automating them, it guarantees that coders
  56. will not accidentally get them wrong.  (Coders can, of course, still
  57. get the *specifications* wrong, and in C's favor, C++ specifications
  58. are rather more complex.)
  59.  
  60. In addition, doing C++-style inheritence in plain C (whether single or
  61. multiple---both are possible) requires rather extensive use of casts
  62. and is generally far less convenient and more error-prone.  To some
  63. extent, comparisons between C++ and C are like those between C and
  64. assembly.  I myself am not entirely happy with C++, but I readily
  65. acknowledge that it is superior to C in a number of ways for a number
  66. of tasks.  (On the other hand, for some tasks assembly is superior to
  67. both---none of these situations are entirely one-sided.)
  68. -- 
  69. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  70. Berkeley, CA        Domain:    torek@ee.lbl.gov
  71.