Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!brutus.cs.uiuc.edu!lll-winken!elroy.jpl.nasa.gov!ames!pacbell!hoptoad!farcomp!andrewjp From: andrewjp@farcomp.UUCP (Andrew Peterson) Newsgroups: comp.lang.c++ Subject: Re: Is there a way to tell which derived class an object belongs to? Message-ID: <138@farcomp.UUCP> Date: 1 Feb 90 01:20:48 GMT References: <2815@draken.nada.kth.se> Reply-To: andrewjp@farcomp.UUCP (Andrew Peterson) Organization: Farallon Computing Inc. Berkeley, CA Lines: 25 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