static_cast etc

Discussion in 'C++' started by shrishjain@gmail.com, Mar 14, 2005.

  1. Guest

    Hi All,

    Do people frequently use static_cast, const_cast etc in industry?.. I
    only saw them in books, and never in real code..

    Shrish
     
    , Mar 14, 2005
    #1
    1. Advertising

  2. * :
    >
    > Do people frequently use static_cast, const_cast etc in industry?.. I
    > only saw them in books, and never in real code..


    Then you haven't seen real code.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 14, 2005
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > Hi All,

    Greetings, one.
    > Do people frequently use static_cast, const_cast etc in industry?.. I
    > only saw them in books, and never in real code..



    I've seen it used in "industry" and real code.
    I have also seen people who were not yet
    using them become convinced that it was
    a good idea to begin.

    If you are working in shops where nobody
    has seen the virtues of modern C++ casts,
    or where they are too stubborn to adopt
    them, then you might want to consider a
    few changes. This may be one of many
    opportunities to lead rather than follow.

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Mar 14, 2005
    #3
  4. Howard Guest

    <> wrote in message
    news:...
    > Hi All,
    >
    > Do people frequently use static_cast, const_cast etc in industry?.. I
    > only saw them in books, and never in real code..
    >
    > Shrish
    >


    To be honest, I still have trouble forcing myself to use static_cast instead
    of the old C-style cast, since I've been doing it the old way for so long.
    But if a cast is neccessary, and the proper cast for the situation is
    static_cast, then your only other choice is a C-style cast, right? And
    that's a worse option, not a better one.

    So the question is, where is static_cast needed?

    Well, one place I need it often is to force my mathematical calculations to
    use a specific numeric type. For example, if I'm dividing two integers, but
    I need a floating-point result, I need to either use a cast, or else assign
    one of those values to a temporary float (or double) variable. A cast is
    easier. (If I don't do this, then the division is integer division, which
    loses the real portion of the answer!)

    The other place I use it often is in callback functions (esp. when dealing
    with the Windows API). In those functions, there is often a void* pointer
    that is passed to my function, which I set up however I see fit when I
    specify the callback to the OS. Usually I set the pointer to the value of
    "this", so that the object who needs to be "called back" is identified. The
    callback function then uses a static_cast to turn that void* into a pointer
    to a pointer to my class type, so that I can then call a member function of
    the object itself to do the actual work of the callback. This mechanism
    allows me to make callbacks into C++ objects, using a "generic" C-style API,
    which has no knowledge of my C++ class types.

    There are obviously other cases where static_cast is used, but these are my
    most common uses of it.

    -Howard
     
    Howard, Mar 15, 2005
    #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. Robin Cull
    Replies:
    5
    Views:
    437
    Andrew Dalke
    Jul 31, 2003
  2. Bo Peng
    Replies:
    11
    Views:
    1,111
    Victor Bazarov
    Oct 20, 2006
  3. Replies:
    5
    Views:
    550
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    438
    Fredrik Lundh
    Aug 13, 2008
  5. junyangzou
    Replies:
    13
    Views:
    277
Loading...

Share This Page