Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cme!cam!ARTEMIS From: miller@FS1.cam.nist.gov (Bruce R. Miller) Newsgroups: comp.lang.c++ Subject: Virtual things, lists, collections, ... Message-ID: <2885836642@ARTEMIS.cam.nist.gov> Date: 13 Jun 91 21:17:22 GMT Sender: news@cam.nist.gov Followup-To: comp.lang.c++ Organization: NIST - Computing and Applied Mathematics Laboratory Lines: 49 All this talk about various ways to do lists & collections, type casting vs. sub-classes vs. templates, has got me wondering. First off, I hope that I am not approaching this thing from either a too naive standpoint, or too biased (I'm a hard-core lisper, flavors & CLOS). Maybe I'd understand it if I studied the books some more, but... Just how is it that Virtual functions work? ie. Where's the type info at run type stored. Do the objects have (hidden) headers? Suppose I've defined a class BASE which has a function which does something, say SCAN, in a generic way and invokes one virtual function within it, say VIRT. An arbitrary number of sub-classes later, say class Z, I define the virtual function for a Z. As I understand it the code for SCAN is not replicated (there's only one version in the object code). So somehow that code (or some dispatch code for VIRT) figures out what the arg is and dispatches. How? I can think of two ways offhand; 1) every class instance has a header with its type encoded -- which might be nice to know if you are worried about tradeoffs of space -- and that might only be needed if the class (or its children?) define virtuals (can the compiler tell?). 2) whenever a virtual (or caller of virtual! or caller of....) is called an extra type code is pushed on the stack (again, can the compiler tell?). Is either of these the `right' way? or both? (so long as it works, it doesn't matter...) At least if 1) were the answer, then it seems that a lot of the problems of collections are simplified if you've got ONE base class for everything you are going to use. And, you can easily have an individual container which contains various objects of _different_ classes in the same list. [Natural for lispers, but nobody seems to be considering that here] But, I understand the problem there: C++ declined to define a single base class that EVERYTHING is an instance of (like class T in CLOS). So everybody makes their own; Every library I've broused defines its OWN class GenericThingy! Gets messy! If C++ would define it, that would seem to handle half the cases people seem to want templates for, doesn't it? Templates vs GenericThingy classes seems to be a tradeoff with: Template vs. GenericThingy replicated object code vs. not replicated. `fast' function call vs. dispatch Missing anything? OK, I've rambled on, speculated, wondered outloud... Flame me or congratulate me on my insight, but at least, TEACH ME!