static_cast<>

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

  1. alg

    alg Guest

    I read somewhere that static_cast<> is not safe, sometimes fatal. Could
    someone tell me why it is still around and when it should be used in
    general? When will it be unsafe to use it?

    Thanks for your help!
    alg, Jul 14, 2003
    #1
    1. Advertising

  2. alg

    David White Guest

    alg <> wrote in message
    news:NFrQa.53763$...
    > I read somewhere that static_cast<> is not safe, sometimes fatal. Could
    > someone tell me why it is still around and when it should be used in
    > general?


    An example of a safe usage is downcasting a base-class pointer to a
    derived-class pointer when you absolutely _know_ that the pointer will
    really point to an instance of the derived class after the cast. You might
    choose static_cast over dynamic_cast in such as case for greater execution
    speed. (Whether you could have avoided the downcast altogether is another
    matter. Downcasts are distasteful.)

    > When will it be unsafe to use it?


    When there is no sensible conversion to the destination type. If you have a
    Shape* that you think points to what was created as a Rectangle, then it
    would be unsafe to static_cast it to an Ellipse*, for example. Since
    programmers generally don't do such things deliberately, unsafe usage would
    nearly always result from the programmer's misunderstanding of what the
    object can be safely converted to. In this respect static_cast is unsafe
    because the programmer is saying to the compiler, "trust me", but now and
    then that trust is misplaced, and a bug or potential bug is the result.

    DW
    David White, Jul 14, 2003
    #2
    1. Advertising

  3. alg

    alg Guest

    David White <> wrote in message
    news:mxsQa.352$...
    > alg <> wrote in message
    > news:NFrQa.53763$...
    > > I read somewhere that static_cast<> is not safe, sometimes fatal. Could
    > > someone tell me why it is still around and when it should be used in
    > > general?

    >
    > An example of a safe usage is downcasting a base-class pointer to a
    > derived-class pointer when you absolutely _know_ that the pointer will
    > really point to an instance of the derived class after the cast. You might
    > choose static_cast over dynamic_cast in such as case for greater execution
    > speed. (Whether you could have avoided the downcast altogether is another
    > matter. Downcasts are distasteful.)
    >

    Thanks, David!

    How do "you absolutely _know_ that the pointer will really point to an
    instance of the derived class after the cast"? By checking the return value
    to see if it's NULL or not?
    alg, Jul 14, 2003
    #3
    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. Chandra Shekhar Kumar

    static_cast confusion

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    7
    Views:
    2,006
    Mirek Fidler
    Jun 27, 2003
  2. Wenjie
    Replies:
    4
    Views:
    411
    Victor Bazarov
    Jul 12, 2003
  3. Alan Sung

    Re: static_cast question

    Alan Sung, Aug 2, 2003, in forum: C++
    Replies:
    0
    Views:
    486
    Alan Sung
    Aug 2, 2003
  4. Bo Peng
    Replies:
    11
    Views:
    1,071
    Victor Bazarov
    Oct 20, 2006
  5. junyangzou
    Replies:
    13
    Views:
    247
Loading...

Share This Page