[comp.lang.c++] Is there a way to tell which derived class an object belongs to?

andrewjp@farcomp.UUCP (Andrew Peterson) (02/01/90)

matsl@nada.kth.se asks whether there's a way to tell what subclass
of a baseclass an object is.  In other words, is there a method to
return the class of an object.  Clearly the answer is no.

As you probably know, most other object-oriented systems have 
such a mechanism, and it's easily put into C++ where it's needed.
Simply define a base class:
class MyBase {
	short type;
public: GetType( short ); SetType( short ); };

Use this base class as your base class.  Assign each class you're interested 
in an id (in a common header file), and in the constructor for the object,
call the SetType method.

Obviously this is a bit clunky, but it should work fine.  Apple's MacApp
provides such functionality in their base class TObject, including functions
to compare objects and find all the subclasses or superclasses.

I believe Objective C uses a similar naming scheme.

Andrew

apple!farcomp!andrewjp
 

sakkinen@tukki.jyu.fi (Markku Sakkinen) (02/01/90)

In article <7722@chaph.usc.edu> jeenglis@nunki.usc.edu (Joe English) writes:
>kempf@tci.bell-atl.com (Cory Kempf) writes:
>>matsl@nada.kth.se (Mats Luthman) writes:
> ...
>An earlier poster (Mats Luthman, matsl@nada.kth.se)
>came up with a nice way to do it, though:
>have a tag() function in each derived class 
>returning the address of a private static variable;
>the linker will generate unique ID's then
>no matter how many new types are introduced.

Since Luthman's scheme as such uses only the _address_ of that private
static variable, here we have a case where a variable of type _void_
would be optimal! Of course, the variable could well be of string type
and contain the name of the class.

A drawback of Luthman's scheme is that the programmer must remember
to really redefine the class-identifying function in every subclass
(derived class); otherwise the objects will seem to belong to the
superclass (base class) at least in the case of single inheritance.
With multiple immediate base classes, I suppose the compiler will
give an error message about ambiguous definitions if the programmer
forgets the redefinition.

Markku Sakkinen
Department of Computer Science
University of Jyvaskyla (a's with umlauts)
Seminaarinkatu 15
SF-40100 Jyvaskyla (umlauts again)
Finland

duvarney@yoyodyne.rtp.dg.com (Dan DuVarney) (02/05/90)

If you are interested in telling which derived class an object belongs to, 
then you should check out Keith Gorlen's OOPS library.

OOPS defines a class "Class" which contains the name, inheritance, etc. for a 
given class. For each OOPS class, there is a corresponding member of Class,
usually called "class_<class-name>."

There is a virtual function "isA()" for each object that returns a 
reference to the appropriate Class variable. This scheme is more complicated 
than type-id numbers, but has the advantage that you can ask questions like 
"is this object a member of a class derived from the class 'Set?'"

Dan DuVarney      ...!mcnc!rti!dg-rtp!duvarney