Path: utzoo!utgpu!watserv1!watmath!att!occrsh!uokmax!munnari.oz.au!samsung!zaphod.mps.ohio-state.edu!rpi!ptolemy1.rdrc.rpi.edu!amehta From: amehta@ptolemy1.rdrc.rpi.edu Newsgroups: comp.lang.c++ Subject: Can a base class know which derived class is constructed? Message-ID: Date: 12 Sep 90 12:56:54 GMT Reply-To: amehta@ptolemy1.rdrc.rpi.edu () Organization: Rensselaer Polytechnic Institute, Troy NY Lines: 48 Hi, Sorry if this has been asked before! I have a base class, say BASE, and several derived classes. Can the base class know which derived class is being created at the time of construction? I want to avoid passing parameters if possible. I tried a virtual identity method, but the method becomes valid only AFTER construction. Consider: /*---------------------------------------------------------*/ #include class BASE { public: BASE () { printf ("Cons: BASE\n"); identify(); } virtual void identify () { printf ("Ident: I am a BASE\n"); } }; class DER1 : public BASE { public: DER1 () { printf ("Cons: DER1\n"); identify(); } virtual void identify () { printf ("Ident: I am a DER1\n"); } }; class DER2 : public DER1 { public: DER2 () { printf ("Cons: DER2\n"); identify(); } virtual void identify () { printf ("Ident: I am a DER2\n"); } }; main() { printf ("Starting\n"); DER2 d; d.identify(); printf ("Done\n"); } /*-------- This program produces as output: -------*/ Starting Cons: BASE Ident: I am a BASE Cons: DER1 Ident: I am a DER1 Cons: DER2 Ident: I am a DER2 Ident: I am a DER2 Done /*-------------------------------------------------*/ Any help would be much appreciated! Thanks Alok