public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: John Love-Jensen <eljay@adobe.com>
To: Eilmsteiner Reinhard <r.eilmsteiner@paysafecard.com>,
	"'gcc-help@gcc.gnu.org'" <gcc-help@gcc.gnu.org>
Subject: Re: Determining object's class name at runtime
Date: Wed, 05 Feb 2003 15:49:00 -0000	[thread overview]
Message-ID: <BA668D6E.720C%eljay@adobe.com> (raw)
In-Reply-To: <23FD0445E7F1D4119D910002A50981138D1997@s-vie1-11.psc.com>

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<A*>(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

      reply	other threads:[~2003-02-05 15:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-05 15:02 Eilmsteiner Reinhard
2003-02-05 15:49 ` John Love-Jensen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BA668D6E.720C%eljay@adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=r.eilmsteiner@paysafecard.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).