home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: General Datastructure Design Question
- Message-ID: <TMB.92Sep9143752@arolla.idiap.ch>
- Date: 9 Sep 92 18:37:52 GMT
- References: <1992Sep8.114516.4105@mic.ucla.edu>
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 48
- In-reply-to: iwelch@agsm.ucla.edu's message of 8 Sep 92 18:45:16 GMT
-
- In article <1992Sep8.114516.4105@mic.ucla.edu> iwelch@agsm.ucla.edu (Ivo Welch) writes:
-
- |I am trying to define a simple data structure, like a binary tree, which can
- |be used by any other data structures. So, I decided to go the C++ way, rather
- |than passing byte sizes of structures as I would in C.
- |
- |My guess was that a good design would define an abstract "TreeNode" class on
- |which my BinaryTree class would operate. This abstract TreeNode class
- |declares 2 functions, (fix, compare). A user would define her own "Node"
- |class, inheriting the abstract class, which [a] contains the data and [b]
- |defines the fix() [=copy] and compare() functions. The instantiated user-node
- |object carries with it the information of what compare and fix functions
- |operate on it, because it is derived from a virtual abstract class.
-
- Personally, I don't like that style of programming. A TreeNode
- containing a Foo should not be the same as a TreeNode inheriting from
- a Foo.
-
- It may be better simply to "simulate" templates until you get a
- version of GNU C++ that supports them. There are two common ways of
- doing this (of which the latter doesn't seem to be well-known):
-
- (1) Using "#define":
-
- #define make_Array_type(BaseType) \
- struct Array { \
- BaseType *data; \
- ... \
- };
-
- (2) Using "#include" (much easier to debug):
-
- ===> generic-array.h:
-
- struct Array {
- BaseType *data;
- ...
- };
-
- ===> using generic-array.h
-
- #define BaseType int
- #define Array IntArray
- #include "generic-array.h"
- #undef Array
- #undef BaseType
-
- Thomas.
-