RTTI typeid question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    RTTI typeid question

    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
    }


  • Victor Bazarov

    #2
    Re: RTTI typeid question

    Mike wrote:[color=blue]
    > 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. "[/color]

    Your class 'foot' does not have virtual functions.
    [color=blue]
    >
    > example
    >
    > class foo
    > {
    > void create();
    > void virtfunction()= 0;[/color]

    Did you mean to say

    virtual void virtfunction()= 0;

    ?
    [color=blue]
    > const char *m_type;
    > };
    >
    > class x : public foo
    > {
    > create();
    > void virtfunction;[/color]

    Did you mean to say

    void virtfunction();

    ??
    [color=blue]
    > }
    >
    > 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
    > }[/color]

    Try to always post the _actual_code_ that doesn't work or doesn't
    compile, instead of typing some nonsense while in your newsreader.

    Victor

    Comment

    • Carl Ribbegaardh

      #3
      Re: RTTI typeid question

      Try: typeid(*this)

      :)

      "Mike" <mike@somewhere .com> wrote in message
      news:boMtc.1488 8$Ly.14192@attb i_s01...[color=blue]
      > I want to use typeid() in a base class function to determine the name of[/color]
      the[color=blue]
      > derived class. typeid(this) returns the name of the base class (which is[/color]
      an[color=blue]
      > 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[/color]
      of[color=blue]
      > 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[/color]
      do[color=blue]
      > foo::create();
      > }
      >
      > void x::virtfunction ()
      > {
      > // does something
      > }
      >
      >[/color]


      Comment

      • Mike

        #4
        Re: RTTI typeid question

        Thanks, Carl. That did the trick.

        "Carl Ribbegaardh" <carl_ribbegaar dh.nospam@hotma il.com> wrote in message
        news:2hpkd0Feeg 56U1@uni-berlin.de...[color=blue]
        > Try: typeid(*this)
        >
        > :)
        >
        > "Mike" <mike@somewhere .com> wrote in message
        > news:boMtc.1488 8$Ly.14192@attb i_s01...[color=green]
        > > I want to use typeid() in a base class function to determine the name of[/color]
        > the[color=green]
        > > derived class. typeid(this) returns the name of the base class (which[/color][/color]
        is[color=blue]
        > an[color=green]
        > > abstract class) rather than the derived class. I'd rather not require[/color][/color]
        the[color=blue][color=green]
        > > 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[/color][/color]
        actually[color=blue]
        > of[color=green]
        > > a type derived from that base class, a type_info reference for the[/color][/color]
        derived[color=blue][color=green]
        > > class is the result. The expression must point to a polymorphic type,[/color][/color]
        that[color=blue][color=green]
        > > 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[/color]
        > do[color=green]
        > > foo::create();
        > > }
        > >
        > > void x::virtfunction ()
        > > {
        > > // does something
        > > }
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...