dynamic_cast<>

Discussion in 'C++' started by alg, Jul 14, 2003.

  1. alg

    alg Guest

    dynamic_cast<> comes in play when to perform conversion from a pointer to a
    base class to a pointer to a derived class.

    I don't understand:

    1. why this is so necessary since we can either use an explicit cast or use
    "static_cast<>" (Can we?)?
    2. the above said conversion is dangerous, using explicit conversion. Does
    this mean that using dynamic_cast<> will be safer. How this could be?
    3. Is static_cast<> at compiling time and dynamic_cast<> realized at
    runtime?
     
    alg, Jul 14, 2003
    #1
    1. Advertising

  2. "alg" <> wrote in message
    news:2OrQa.53769$...
    > dynamic_cast<> comes in play when to perform conversion from a pointer to

    a
    > base class to a pointer to a derived class.
    >
    > I don't understand:
    >
    > 1. why this is so necessary since we can either use an explicit cast or

    use
    > "static_cast<>" (Can we?)?


    See below.

    > 2. the above said conversion is dangerous, using explicit conversion. Does
    > this mean that using dynamic_cast<> will be safer. How this could be?


    Because dynamic_cast will return NULL if the cast cannot be safely made.
    static_cast will do the cast anyway. Try this

    struct B { virtual ~B() {} };
    struct D : B {};

    int main()
    {
    B* b = new B;
    D* d1 = static_cast<D*>(b); // unsafe
    D* d2 = dynamic_cast<D*>(b); // safe, returns NULL
    cout << d1 << ' ' << d2 << endl;
    }

    > 3. Is static_cast<> at compiling time and dynamic_cast<> realized at
    > runtime?
    >


    Yes.
     
    John Harrison, Jul 14, 2003
    #2
    1. Advertising

  3. On Mon, 14 Jul 2003 06:09:02 GMT, "alg" <> wrote:

    >dynamic_cast<> comes in play when to perform conversion from a pointer to a
    >base class to a pointer to a derived class.


    And in other situations, but where did you get this sentence?


    >I don't understand:
    >
    >1. why this is so necessary since we can either use an explicit cast or use
    >"static_cast<>" (Can we?)?


    A dynamic cast _is_ explicit.


    >2. the above said conversion is dangerous, using explicit conversion. Does
    >this mean that using dynamic_cast<> will be safer. How this could be?
    >3. Is static_cast<> at compiling time and dynamic_cast<> realized at
    >runtime?


    A dynamic cast performs runtime checking whenever needed, but that doesn't
    mean that it does so when all necessary type information is available
    at compile time, or (especially) when there is no runtime type info.

    Do check this out in your documentation or the C++ standard.
     
    Alf P. Steinbach, Jul 14, 2003
    #3
  4. alg

    Rolf Magnus Guest

    alg wrote:

    > dynamic_cast<> comes in play when to perform conversion from a pointer
    > to a base class to a pointer to a derived class.
    >
    > I don't understand:
    >
    > 1. why this is so necessary since we can either use an explicit cast
    > or use "static_cast<>" (Can we?)?


    To find out if the object can really be used as one of the specified
    derived class.
    Btw, your terminology "explicit cast" is not very good. First, all casts
    are explicit, since that's their nature. A cast is what you write into
    your code to explicitly invoke a conversion. This of course also means
    that dynamic_cast _is_ an "explicit cast", just like all other casts.

    > 2. the above said conversion is dangerous, using explicit conversion.


    Casts should be avoided if possible, but if you need a downcast on
    polymorphic types, dynamic_cast is the safest one.

    > Does this mean that using dynamic_cast<> will be safer.


    Than what? static_cast? Depending on the situation, it can be safer.

    > How this could be?


    dynamic_cast returns 0 (when used on pointers) or throws an exception
    (when used on references) if the object is actually not of the
    specified class.

    > 3. Is static_cast<> at compiling time and dynamic_cast<> realized at
    > runtime?


    Yes.
     
    Rolf Magnus, Jul 14, 2003
    #4
    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. Dan Noland

    dynamic_cast and references

    Dan Noland, Jul 29, 2003, in forum: C++
    Replies:
    0
    Views:
    503
    Dan Noland
    Jul 29, 2003
  2. Yuming Ma
    Replies:
    1
    Views:
    714
    Jeff Schwab
    Dec 17, 2003
  3. Andreas Sch.

    typeid and dynamic_cast, gcc 3.3

    Andreas Sch., Jan 23, 2004, in forum: C++
    Replies:
    18
    Views:
    1,900
    Janusz Szpilewski
    Jan 29, 2004
  4. Jamie Burns
    Replies:
    11
    Views:
    9,042
    Nick Hounsome
    Jan 29, 2004
  5. exits funnel
    Replies:
    2
    Views:
    598
    exits funnel
    Feb 25, 2004
Loading...

Share This Page