About RTTI

Discussion in 'C++' started by Steven Lien, Aug 19, 2003.

  1. Steven Lien

    Steven Lien Guest

    Hi all

    As far as i know, there has 2 ways RTTI in C++
    one is dynamic_cast and another is typeid

    Since, my book only pointed me that to use "typeid" and "static_cast"
    conjunction will be much more efficient than "dynamic_cast",
    but what the book does not provide any evidence.

    So my question can any explain to me why dynmic_cast is slower than typeid??

    And since it's much slower, why not simply throw it away


    Any Help will be appreciated
     
    Steven Lien, Aug 19, 2003
    #1
    1. Advertising

  2. Steven Lien

    Buster Guest

    "Steven Lien" <> wrote in message
    news:bhtfdi$cjl$...
    > Hi all
    >
    > As far as i know, there has 2 ways RTTI in C++
    > one is dynamic_cast and another is typeid
    >
    > Since, my book only pointed me that to use "typeid" and "static_cast"
    > conjunction will be much more efficient than "dynamic_cast",
    > but what the book does not provide any evidence.


    I don't think that's true in general. In the example given in your book,
    is the pointer type-checked once then used many times? If so, a
    static_cast to the known object type will be faster than repeated
    dynamic_casts. But I would use dynamic_cast to perform the check
    in the beginning, unless there were a good reason not to.

    > So my question can any explain to me why dynmic_cast is slower than typeid??
    > And since it's much slower, why not simply throw it away
    >
    > Any Help will be appreciated
     
    Buster, Aug 19, 2003
    #2
    1. Advertising

  3. "Steven Lien" <> wrote...
    > As far as i know, there has 2 ways RTTI in C++
    > one is dynamic_cast and another is typeid
    >
    > Since, my book only pointed me that to use "typeid" and "static_cast"
    > conjunction will be much more efficient than "dynamic_cast",
    > but what the book does not provide any evidence.


    Could it be because it's really nonsense?

    > So my question can any explain to me why dynmic_cast is slower than

    typeid??

    No. Because there is no such requirement or any evidence of that
    in the language definition. You have to ask the authors of the
    book you're referring to.

    > And since it's much slower, why not simply throw it away


    Again, I am not sure where you got that "much slower" nonsense,
    but I believe they both are in the language because they serve
    different purposes.

    Victor
     
    Victor Bazarov, Aug 19, 2003
    #3
  4. Steven Lien

    Mike Wahler Guest

    Steven Lien <> wrote in message
    news:bhtfdi$cjl$...
    > Hi all
    >
    > As far as i know, there has 2 ways RTTI in C++
    > one is dynamic_cast and another is typeid
    >
    > Since, my book only pointed me that to use "typeid" and "static_cast"
    > conjunction will be much more efficient than "dynamic_cast",
    > but what the book does not provide any evidence.


    Which book and author? Perhaps you need a better one.

    -Mike
     
    Mike Wahler, Aug 19, 2003
    #4
  5. "Steven Lien" <> wrote in message
    news:bhtfdi$cjl$...
    | As far as i know, there has 2 ways RTTI in C++
    | one is dynamic_cast and another is typeid
    |
    | Since, my book only pointed me that to use "typeid" and "static_cast"
    | conjunction will be much more efficient than "dynamic_cast",
    | but what the book does not provide any evidence.
    |
    | So my question can any explain to me why dynmic_cast is slower than
    typeid??
    |
    | And since it's much slower, why not simply throw it away

    Slower at what ? They serve very different purposes.
    Consider:

    #include <typeinfo>

    class One { public: virtual ~One(){} };
    class Two : public One {};
    class Three : public Two {};

    void isThisATwo(One* p)
    {
    // which one of the following values do you want ???
    bool same1 = ( 00 != dynamic_cast<Two*>(p) );
    bool same2 = ( typeid(*p)==typeid(Two) );
    }

    int main()
    {
    Three p;
    isThisATwo(&p); // which result do you want?
    }

    dynamic_cast does a more exhaustive search, to tell you
    whether an instance is of a specific type, *OR* any type
    derived from it. typeid() cannot provide this information.



    Additionally, dynamic_cast can perform casts that are
    not accessible to static_cast:
    class Base1 { public: virtual ~Base1(){} };
    class Base2 { public: virtual ~Base2(){} };
    class Derived : public Base1, public Base2 {};

    void f(Base1* p1)
    {
    // can't be done with a static_cast...
    Base2* p2 = dynamic_cast<Base2*>(p1);
    }

    int main()
    {
    Derived d;
    f( &d );
    }


    Make sure to read other books about C++...


    Regards,
    --
    Ivan Vecerina <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Aug 19, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: RTTI

    John Harrison, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    453
    John Harrison
    Jul 14, 2003
  2. Alf P. Steinbach

    Re: RTTI

    Alf P. Steinbach, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    469
    Alf P. Steinbach
    Jul 14, 2003
  3. BillyO
    Replies:
    2
    Views:
    490
    dslater
    Sep 30, 2003
  4. tirath
    Replies:
    3
    Views:
    712
    Ivan Vecerina
    Oct 12, 2003
  5. MoCha

    using rtti

    MoCha, Nov 26, 2003, in forum: C++
    Replies:
    1
    Views:
    382
    Ivan Vecerina
    Nov 26, 2003
Loading...

Share This Page