ericg@ucschu.ucsc.edu (Eric Goodman) (03/17/91)
I apologize if this is the same question that sparked the "re:asking an object for it's type" debate, I've fallen behind and only caught the last few posts on the topic. I want to use a comon base class for many of my data types. The purpose of this is to create a class library similar to the one that comes with Turbo C++, where Object is the base class of all other classes. The purpose of this is to be able to create container classes, etc. that need only be defined once, such as: class List; class ListNode { friend class List; Object *data; ListNode *next; /* constructors, destructors, etc. */ }; class List { ListNode *list; insert(Object*); remove(Object*); /* and a bunch more functions that just take Object*'s and Object&'s */ }; In order to implement this scheme there has to be some way to verify that two objects are equal. When I call List::remove(Object*); there has to be some way to determine the actual type of the Object I pass in and the Objects already in the list so that the correct comparison semantics can be used. I don't want to use a method of including an isKindof() function to check this, but I don't see how to otherwise have such a generic list work. (Please enlighten me as to other methods) I have tried a scheme where there is a class GenericList that has protected insert(Object*) and length(Object*) and there are derived classes IntList and StringList, etc, that provide their own insert(int) members that force you to send in the correct data type (then the class IntList can correctly assume that any Object in it's list is really an int) but that seems inelegant. Besides which, I still eventually have to cast my Objects back to what they actually are to compare them. Parameterized typing or generics.h might reduce the amount of typing I do, but I still seem to be stuck casting an Object to a derived type. The question: is there a way to get such a generic list without doing some explicit casting up the inheritance hierarchy or otherwise being forced to know to much about an object? Virtual functions work fine for when you are calling a procedure through an object, but what mechanism do you use when the type of the *actual* argument being passed affects the meaning of the function call, but the actual type is lost due to a cast to a base class? (Like when you call List::remove(Object* obj), and obj's actual type determines the comparison semantics) Any help will be appreciated. Please e-mail responses as I don't get to keep up as much as I like to . Eric Goodman, UC Santa Cruz ericg@ucschu.ucsc.edu or @ucschu.bitnet Eric_Goodman.staff@macmail.ucsc.edu