home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13424 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.0 KB  |  63 lines

  1. Path: sparky!uunet!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: General Datastructure Design Question
  5. Message-ID: <TMB.92Sep9143752@arolla.idiap.ch>
  6. Date: 9 Sep 92 18:37:52 GMT
  7. References: <1992Sep8.114516.4105@mic.ucla.edu>
  8. Sender: news@ai.mit.edu
  9. Reply-To: tmb@idiap.ch
  10. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  11.     Perceptive)
  12. Lines: 48
  13. In-reply-to: iwelch@agsm.ucla.edu's message of 8 Sep 92 18:45:16 GMT
  14.  
  15. In article <1992Sep8.114516.4105@mic.ucla.edu> iwelch@agsm.ucla.edu (Ivo Welch) writes:
  16.  
  17. |I am trying to define a simple data structure, like a binary tree, which can
  18. |be used by any other data structures. So, I decided to go the C++ way, rather
  19. |than passing byte sizes of structures as I would in C.
  20. |
  21. |My guess was that a good design would define an abstract "TreeNode" class on
  22. |which my BinaryTree class would operate. This abstract TreeNode class
  23. |declares 2 functions, (fix, compare). A user would define her own "Node"
  24. |class, inheriting the abstract class, which [a] contains the data and [b]
  25. |defines the fix() [=copy] and compare() functions. The instantiated user-node
  26. |object carries with it the information of what compare and fix functions
  27. |operate on it, because it is derived from a virtual abstract class.
  28.  
  29. Personally, I don't like that style of programming. A TreeNode
  30. containing a Foo should not be the same as a TreeNode inheriting from
  31. a Foo.
  32.  
  33. It may be better simply to "simulate" templates until you get a
  34. version of GNU C++ that supports them. There are two common ways of
  35. doing this (of which the latter doesn't seem to be well-known):
  36.  
  37. (1) Using "#define":
  38.  
  39. #define make_Array_type(BaseType) \
  40. struct Array { \
  41.     BaseType *data; \
  42.     ... \
  43. };
  44.  
  45. (2) Using "#include" (much easier to debug):
  46.  
  47. ===> generic-array.h:
  48.  
  49. struct Array {
  50.     BaseType *data;
  51.     ...
  52. };
  53.  
  54. ===> using generic-array.h
  55.  
  56. #define BaseType int
  57. #define Array IntArray
  58. #include "generic-array.h"
  59. #undef Array
  60. #undef BaseType
  61.  
  62.                 Thomas.
  63.