From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26640 invoked by alias); 5 Feb 2003 15:49:27 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 26633 invoked from network); 5 Feb 2003 15:49:25 -0000 Received: from unknown (HELO smtp-relay-2.adobe.com) (192.150.11.2) by 172.16.49.205 with SMTP; 5 Feb 2003 15:49:25 -0000 Received: from inner-relay-1.corp.adobe.com (inner-relay-1 [153.32.1.51]) by smtp-relay-2.adobe.com (8.12.3/8.12.3) with ESMTP id h15FnLAC012266 for ; Wed, 5 Feb 2003 07:49:22 -0800 (PST) Received: from iplan-mn.corp.adobe.com (iplan-mn.corp.adobe.com [130.248.25.5]) by inner-relay-1.corp.adobe.com (8.12.3/8.12.3) with ESMTP id h15FnGcO010690 for ; Wed, 5 Feb 2003 07:49:17 -0800 (PST) Received: from [130.248.25.220] ([153.32.12.150]) by iplan-mn.corp.adobe.com (Netscape Messaging Server 4.15 mn Jul 11 2001 16:32:57) with ESMTP id H9UEM400.JJ0; Wed, 5 Feb 2003 09:49:16 -0600 User-Agent: Microsoft-Entourage/10.0.0.1331 Date: Wed, 05 Feb 2003 15:49:00 -0000 Subject: Re: Determining object's class name at runtime From: John Love-Jensen To: Eilmsteiner Reinhard , "'gcc-help@gcc.gnu.org'" Message-ID: In-Reply-To: <23FD0445E7F1D4119D910002A50981138D1997@s-vie1-11.psc.com> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-SW-Source: 2003-02/txt/msg00056.txt.bz2 Hi Reinhard, >Is there a way (with gcc 3.2) to determine the class name of an object at runtime? Yes. Given your example: class A; class B : public A; const type_info* tA = &typeid(*pA); // Get the type information. cout << tA->name(); // Textual (probably mangled) CLASSNAME. bool match = dynamic_cast(pA) != NULL; // Check ISKINDOF. All these are explained in Section 15.4 of Stroustrup's C++PL (3rd ed or Special ed). If you find that you need to use these kinds of constructs a lot, you may want to consider using a more appropriate language or reconsider your applications design. It may be that you are not using the OO facilities as much as you should. (If you need this mechanism for callbacks or passing things through the OS API or across a C ABI, then that's apropos.) Perl, Java, Lisp/Scheme/CLOS, Objective-C are all good choices. --Eljay