typeid and polymorphic classes

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

    typeid and polymorphic classes

    Please consider this code:

    class base {};
    class derived: public base {};

    base *ptr = new derived;
    cout << typeid(*base).n ame << endl;

    In this case, I see output of "class base" rather than "class derived".
    This is somewhat expected I suppose since my base class does not have any
    virtual functions and, therefore, I do not have polymorphic classes.

    Adding a virtual destructor to base results in output of "class derived", as
    expected.

    Can somebody more knowledgeable than I please confirm that this is indeed
    the exact expected behavior according to the C++ Standard?

    Thank you!


  • Attila Feher

    #2
    Re: typeid and polymorphic classes

    Dave Theese wrote:[color=blue]
    > Please consider this code:
    >
    > class base {};
    > class derived: public base {};
    >
    > base *ptr = new derived;
    > cout << typeid(*base).n ame << endl;
    >
    > In this case, I see output of "class base" rather than "class
    > derived". This is somewhat expected I suppose since my base class
    > does not have any virtual functions and, therefore, I do not have
    > polymorphic classes.
    >
    > Adding a virtual destructor to base results in output of "class
    > derived", as expected.
    >
    > Can somebody more knowledgeable than I please confirm that this is
    > indeed the exact expected behavior according to the C++ Standard?[/color]

    It is.

    --
    Attila aka WW


    Comment

    • Duane Hebert

      #3
      Re: typeid and polymorphic classes


      "Dave Theese" <cheeser_1998@y ahoo.com> wrote in message
      news:vlo4tsbser nq0b@news.super news.com...[color=blue]
      > Please consider this code:
      >
      > class base {};
      > class derived: public base {};
      >
      > base *ptr = new derived;
      > cout << typeid(*base).n ame << endl;
      >
      > In this case, I see output of "class base" rather than "class derived".
      > This is somewhat expected I suppose since my base class does not have any
      > virtual functions and, therefore, I do not have polymorphic classes.
      >
      > Adding a virtual destructor to base results in output of "class derived",[/color]
      as[color=blue]
      > expected.
      >
      > Can somebody more knowledgeable than I please confirm that this is indeed
      > the exact expected behavior according to the C++ Standard?[/color]

      It is. BTW, without a virtual dtor, what do you think delete ptr; is going
      to do?


      Comment

      • Dave Theese

        #4
        Re: typeid and polymorphic classes


        "Duane Hebert" <spoo@flarn.com > wrote in message
        news:0BY6b.2725 4$dB3.864760@we ber.videotron.n et...[color=blue]
        >
        > "Dave Theese" <cheeser_1998@y ahoo.com> wrote in message
        > news:vlo4tsbser nq0b@news.super news.com...[color=green]
        > > Please consider this code:
        > >
        > > class base {};
        > > class derived: public base {};
        > >
        > > base *ptr = new derived;
        > > cout << typeid(*base).n ame << endl;
        > >
        > > In this case, I see output of "class base" rather than "class derived".
        > > This is somewhat expected I suppose since my base class does not have[/color][/color]
        any[color=blue][color=green]
        > > virtual functions and, therefore, I do not have polymorphic classes.
        > >
        > > Adding a virtual destructor to base results in output of "class[/color][/color]
        derived",[color=blue]
        > as[color=green]
        > > expected.
        > >
        > > Can somebody more knowledgeable than I please confirm that this is[/color][/color]
        indeed[color=blue][color=green]
        > > the exact expected behavior according to the C++ Standard?[/color]
        >
        > It is. BTW, without a virtual dtor, what do you think delete ptr; is[/color]
        going[color=blue]
        > to do?
        >
        >[/color]

        Undefined, but typically only the base class dtor will be executed.


        Comment

        • Ron Natalie

          #5
          Re: typeid and polymorphic classes


          "Dave Theese" <cheeser_1998@y ahoo.com> wrote in message news:vlo4tsbser nq0b@news.super news.com...
          [color=blue]
          >
          > Adding a virtual destructor to base results in output of "class derived", as
          > expected.[/color]

          Without a virtual method, the class is NOT polymorphic. 5.2.8 says that
          the dynamic type is only checked when it is.


          Comment

          • Govindan

            #6
            Re: typeid and polymorphic classes


            "Dave Theese" <cheeser_1998@y ahoo.com> wrote in message
            news:vlo4tsbser nq0b@news.super news.com...[color=blue]
            > Please consider this code:
            >
            > class base {};
            > class derived: public base {};
            >
            > base *ptr = new derived;
            > cout << typeid(*base).n ame << endl;
            >
            > In this case, I see output of "class base" rather than "class derived".
            > This is somewhat expected I suppose since my base class does not have any
            > virtual functions and, therefore, I do not have polymorphic classes.
            >
            > Adding a virtual destructor to base results in output of "class derived",[/color]
            as[color=blue]
            > expected.
            >
            > Can somebody more knowledgeable than I please confirm that this is indeed
            > the exact expected behavior according to the C++ Standard?
            >
            > Thank you!
            >
            >[/color]

            Hi ,

            Try referring to item 37: Never redefine an inherited non-virtual function
            in the "Effective C++: 50 Specific ways to improve your programs and
            Designs"
            2nd Edition by Scott Meyers.
            Some excerpts:
            ...... nonvirtual functions are statically bound.....on the other hand,
            virtual functions are dynamically bound......
            Also put print statements in your constructors and destructors of your
            classes to see where they are called, which order etc.




            Comment

            Working...