Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!news.funet.fi!funic!fuug!demos!avg From: avg@hq.demos.su (Vadim G. Antonov) Newsgroups: comp.lang.c++ Subject: Re: Angry new C++ programmer: GENERIC CODE? Message-ID: <1990Nov24.113503.27499@hq.demos.su> Date: 24 Nov 90 11:35:03 GMT References: <6907@plains.NoDak.edu> Reply-To: avg@hq.demos.su (Vadim G. Antonov) Organization: DEMOS, Moscow, USSR Lines: 97 In article <6907@plains.NoDak.edu> jmork@plains.NoDak.edu (James Mork) writes: > I am still digging my way into C++ but I am angry to find no >facilities for generic code--as in Ada. Can you do this? Yes, of course. The solution of your problem is quite obvious: USE PREPROCESSOR!!! Example: ------- file btree.h -------- #ifndef tree_type !!!!ERROR: tree_type undefined!!! #endif #define class_name(x) btree_##x class class_name(tree_type) { tree_type value; class_name(tree_type) (unsigned maxnodes); ~class_name(tree_type) (); int add(tree_type value); int delete(tree_type value); int exists(tree_type value); ... } #undef tree_type #undef class_name ------- file btree.cc ------- #ifndef tree_type !!!!ERROR: tree_type undefined!!! #endif #define class_name(x) btree_##x class_name(tree_type)::add(tree_type value) { .... } ....... -------- Makefile -------- BTREEOBJS=btree.int.o btree.float.o # Sorry, I'm too lazy to invent something more valuable... :-) btree.int.o: btree.h btree.cc g++ -c -O -Dtree_type=int btree.cc mv btree.o btree.int.o btree.float.o: btree.h btree.cc g++ -c -O -Dtree_type=float btree.cc mv btree.o btree.float.o yourpgm: $(YOUROBJS) $(BTREEOBJS) ------- Example of usage -------- #define tree_type int #include "btree.h" ..... btree_int *root; >Why didn't they put this in? Because C++ assumes separate linkage. If you're allowing use of generic types you should generate new object code for member functions "on the fly", it requires recompiling the source code of the non-inline class members. Moreover, it may cause some strange situations. Let's imagine you've designed a generic matrix class (say for using with floating-point, fixed-point, rational and Boolean matrices) and someone is trying to use your class over his own cell types WITHOUT operation* ! Generic classes in object-oriented languages is a matter of research now and I think it should not be included in practical language. >It would be nice to be able do define a derived class >class in a similar way as Ada does, however, and just be able to say: It's quite simplier to do in Ada because Ada is far more restrictive than C++, anyway an implementation of generic type in Ada compiler/linker is really tricky. >Maybe I am just not deep enough into this yet... Do not worry (be happy :-), this is a REAL question. Anyway any problem in language can be solved at the level of operating system (all machines are the same and if you can solve problem using one machine/language/ operating system you could do it using another one; time (price :-) is the only thing to worry about). Vadim Antonov DEMOS, Moscow, USSR PS. I'm sorry I haven't tested this example code. Don't blame.