public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Determining object's class name at runtime
@ 2003-02-05 15:02 Eilmsteiner Reinhard
  2003-02-05 15:49 ` John Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Eilmsteiner Reinhard @ 2003-02-05 15:02 UTC (permalink / raw)
  To: 'gcc-help@gcc.gnu.org'

 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Is there a way (with gcc 3.2) to determine the class name of an object at
runtime? Or moreover ist pedigree?
Something like:

...
class A;
class B: public A;
...
A* pA;
...
pA->GetClassName (); or CLASSNAME (pA);
and
pA->IsKindOf("A"); or ISKINDOF(pA, "A");

Thx,
Reinhard Eilmsteiner

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPkEngFaO5+5ahqodEQINlQCcDVuSVmypnQdg1SdrIKBIeTBK3vUAn0e/
uUCrJokIySjDbf9oA979JUtp
=RQCR
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Determining object's class name at runtime
  2003-02-05 15:02 Determining object's class name at runtime Eilmsteiner Reinhard
@ 2003-02-05 15:49 ` John Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2003-02-05 15:49 UTC (permalink / raw)
  To: Eilmsteiner Reinhard, 'gcc-help@gcc.gnu.org'

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-02-05 15:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-05 15:02 Determining object's class name at runtime Eilmsteiner Reinhard
2003-02-05 15:49 ` John Love-Jensen

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).