Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!amdahl!dlb!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c++ Subject: generic types Keywords: generic types class derived Message-ID: <217@goofy.megatest.UUCP> Date: 16 Jan 88 02:59:51 GMT Organization: Megatest Corporation, San Jose, Ca Lines: 61 Greetings++; I've been thinking about how to automate the process of deriving type- secure classes from generic base-classes. I'm not completely happy with the method of doing it that's described in The Book on page 209. As a matter of convention, I would prefer a different style, as illustrated by the following #define from the class-library I am currently building: // A dervied class of Assoc, named _name_, which implements // associative arrays, mapping null-terminated strings to values of // type _value_. #define NAME_TABLE(_name_,_value_) \ class _name_ : public Assoc \ { \ public: \ _name_ () : (sizeof(_value_)) {} \ _value_& operator[] (char* key) \ { return *((_value_*)Assoc::lookup(key));} \ } // note: no semicolon Building the _name_ automatically from the type-names, as on page 209 of The Book, can result in multiple defines. Or, if the user forgets and uses "char*", say, instead of a typename, he will get a jillion nonsense error messages. So, let the user choose the (obligatory) name. Leave off the semicolon. Then you can use it as a class-definition, or as you would a defined type: NAME_TABLE(String_table, char*); // class definition String_table string_table; // uses the derived class // Used like an already defined type... NAME_TABLE(Name_table, struct name_entry) name_table; By the way... point two: I would prefer to be able to define a macro that expands to an anonymous type, then use the macro like a type-descriptor: NAME_TABLE(struct name_entry) lookup_table; But derived classes MUST have names. For one thing, CC complains of syntax-error when the macro expands to, class /* no name */ : public Assoc { public: (struct name_entry)& operator[] (char* key) { return *((_value_*)Assoc::lookup(key));} } lookup_table; Another problem is that the for a class to have a constructor or destructor, the class MUST have a name, so that you can have something to name the constructor or destructor! Okay, so maybe I don't campaign too hard on this second point.