RTTI typeid question

From: Mike (mike_at_somewhere.com)
Date: 05/28/04

  • Next message: JustSomeGuy: "Re: Need help extending stl list."
    Date: Fri, 28 May 2004 19:27:07 GMT
    
    

    I want to use typeid() in a base class function to determine the name of the
    derived class. typeid(this) returns the name of the base class (which is an
    abstract class) rather than the derived class. I'd rather not require the
    typeid call in every derived class implementation. How do I get around
    this?

    I'm depending on the statement in Microsoft Visual Studio documentation
    which states :

    "
    typeid( expression )
    ...
    ...
    If the expression points to a base class type, yet the object is actually of
    a type derived from that base class, a type_info reference for the derived
    class is the result. The expression must point to a polymorphic type, that
    is, a class with virtual functions. "

    example

    class foo
    {
        void create();
        void virtfunction()=0;
        const char *m_type;
    };

    class x : public foo
    {
        create();
        void virtfunction;
    }

    void foo::create()
    {
        const type_info *T = typeid(this); // this produces type
    information for foo, not the derived class!!
       m_type = T->name();
    }

    void x::Create()
    {
        // I'd rather not put the typeid call here although things work if I do
        foo::create();
    }

    void x::virtfunction()
    {
        // does something
    }


  • Next message: JustSomeGuy: "Re: Need help extending stl list."

    Relevant Pages

    • Re: Newbie on Inheritance, Base classes and derived classes
      ... objects retain knowledge of what they really are so you can cast this Watch back to DigitalWatch if needed. ... public void DoStuff ... If I have a base class, and then some derived classes with extra ... Derived Class: DigitalWatch Method ChangeBattery ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: RTTI typeid question
      ... >> typeid call in every derived class implementation. ... >> If the expression points to a base class type, ...
      (comp.lang.cpp)
    • Re: RTTI typeid question
      ... > I want to use typeidin a base class function to determine the name of the ... > abstract class) rather than the derived class. ... > typeid call in every derived class implementation. ... > class x: public foo ...
      (comp.lang.cpp)
    • Re: How Do I Prevent Copying From Base Class?
      ... > three base classes while derived class from base class is executed. ... > void Run; ... I will add them into private class later. ...
      (comp.lang.cpp)
    • Re: RTTI typeid question
      ... typeidreturns the name of the base class (which is ... > abstract class) rather than the derived class. ... > typeid call in every derived class implementation. ...
      (comp.lang.cpp)