home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20276 < prev    next >
Encoding:
Text File  |  1993-01-28  |  3.2 KB  |  94 lines

  1. Xref: sparky comp.lang.c:20276 comp.lang.c++:19960
  2. Path: sparky!uunet!opl.com!hri.com!noc.near.net!news.Brown.EDU!qt.cs.utexas.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Newsgroups: comp.lang.c,comp.lang.c++
  5. Subject: Re: discriminated unions (Re: C/C++ Correctness (was: Re: C/C++ Speed))
  6. Message-ID: <1993Jan26.204305.24612@ucc.su.OZ.AU>
  7. Date: 26 Jan 93 20:43:05 GMT
  8. References: <1993Jan20.184221.19003@netcom.com> <1993Jan21.004105.1335@ucc.su.OZ.AU> <1993Jan21.075642.6482@netcom.com>
  9. Sender: news@ucc.su.OZ.AU
  10. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  11. Lines: 80
  12. Nntp-Posting-Host: extro.ucc.su.oz.au
  13.  
  14. In article <1993Jan21.075642.6482@netcom.com> erc@netcom.com (Eric Smith) writes:
  15. >In article <1993Jan21.004105.1335@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  16. >>    Later I can write a breadth first search.
  17. >>    I dont have to change anything to write the breadth first search.
  18. >>    Its just a global function taking the tree as an argument.
  19. >
  20. >Ok, now I understand.  You want to be able to add functionality to a class
  21. >without changing the class itself nor deriving a new version of it.  
  22.  
  23.     I'm not sure what you mean by 'add functionality'.
  24.  
  25.     The public 'member functions' of a class are the axioms. They are
  26. there to map the private data structure of the class to a public interface.
  27. For reasons of efficiency we sometimes have some coupling between
  28. member functions.
  29.  
  30.     But in general, a function that acts on the public interface
  31. of a class and has no need to know the internal implementation details
  32. should be a global function.
  33.  
  34.     For example:
  35.  
  36.     class complex {
  37.         float real, imaginary;
  38.     public:
  39.         float Real () const;
  40.         float Imag () const;
  41.         float Arg () const;
  42.         float Mod () const;
  43.     };
  44.  
  45. Thats it: now
  46.  
  47.     complex sin(complex);
  48.  
  49. Global function==implmentation independent. Making the complex
  50. 'sin' a member would be wrong in principle and acceptable only
  51. if a there were a signicant gain in efficiency due to accesssing the private
  52. implementation details.
  53.  
  54. >Your
  55. >discriminated unions would be helpful toward that goal, but I still don't
  56. >see any overwhelming advantage of that goal.  The only real advantage I can
  57. >see is that you don't have to use a new class name for your new version.
  58. >
  59. >Other than cluttering class name space, are there any disadvantages in
  60. >deriving new classes to add functionality?
  61.  
  62.     Of course. Unless you adopt a mixin strategy, you cant
  63. use a member function breadth first search on a tree you're
  64. handed which is NOT of the derived type. Thats why you
  65. SHOULD write the searches as global functions, and NOT make
  66. them member functions.
  67.  
  68.     You want your code to be reusable dont you?
  69.  
  70.     class complex_with_trig : public complex {
  71.         complex_with_trig sin()const;
  72.     };
  73.  
  74.     f(complex z)
  75.     {
  76.         ...
  77.         complex_with_trig zz=z;
  78.         complex zzz=zz.sin(); // YUK and inefficient
  79.         ...
  80.     }
  81.  
  82.     f(complex z)
  83.     {
  84.         ...
  85.         complex zzz=sin(z); // nice
  86.         ...
  87.     }
  88.  
  89. -- 
  90. ;----------------------------------------------------------------------
  91.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  92.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  93. ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph:  2 799 8223 --------
  94.