beginner's question on static_cast and const_cast

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 14, 2007.

  1. , India

    , India Guest

    when are the use of static_cast and const_cast essential ?
    Stroustrup, in his book TC++PL(3rd edition) in page number 139
    (in Section 6.5 Advice - [4]), has advised
    "Avoid explicit type conversion(casts)"

    Given this, why do we have static_cast and const_cast
    provided by the language ?

    Kindly explain

    Thanks
    V.Subramanian
     
    , India, Nov 14, 2007
    #1
    1. Advertising

  2. , India

    Jim Langston Guest

    <> wrote in message
    news:...
    > when are the use of static_cast and const_cast essential ?
    > Stroustrup, in his book TC++PL(3rd edition) in page number 139
    > (in Section 6.5 Advice - [4]), has advised
    > "Avoid explicit type conversion(casts)"
    >
    > Given this, why do we have static_cast and const_cast
    > provided by the language ?


    I use a library written by someone else (I don't have the code to it, just
    an interface). Originally he did not write the function calls constant
    correct, so would have things like:

    void drawtext( char* Text, int x, int y );

    Now, I'm using std::strings To use this function with my std::strings poses
    a problem, as a string.c_str() returns a const char*, and you can not call a
    function expecting a non constant pointer with a constant pointer. I
    verified with the developer that the function did not change the string, and
    so I could do this:

    drawtext( const_cast<const char*>( MyString.c_str() ), x, y )

    a bit ugly but it does the trick. It throws away the constantness. Usually
    this would be used when interfacing with C functions or C++ functions that
    are not constant correct. This would do the same thing the C style cast of:

    drawtext( (const char*)MyString.c_str(), x, y )
    would do. But one of the advantages of making so verbose is we want to get
    rid of them. I talked to the developer and he went through and made the
    functions constant correct which allowed me to use:

    drawtext( MyString.c_str(), x, y )

    There are reasons for static cast also, sometimes to just make the compiler
    shut up about warnings without having to disable them, sometimes to cast
    variables explicitly.

    static_cast is not really as dangerous as reinterpret_cast though. Unless
    you know what you are doing and think about all the consenquences
    reinterpret_cast can get you in deep water. I find it useful, however, when
    extracting binary data from some data.
     
    Jim Langston, Nov 14, 2007
    #2
    1. Advertising

  3. , India wrote:
    > when are the use of static_cast and const_cast essential ?
    > Stroustrup, in his book TC++PL(3rd edition) in page number 139
    > (in Section 6.5 Advice - [4]), has advised
    > "Avoid explicit type conversion(casts)"


    He is right. Avoid them if _possible_.

    > Given this, why do we have static_cast and const_cast
    > provided by the language ?


    Sometimes casts are just _necessary_. When you have to use one, you have to
    decide which casting mechanism is appropriate. Const_cast should only be used to
    "cast away" constness. I have only ever seen const_casts in relation to
    non-const correct APIs.

    Static_cast is necessary when you want to down-cast a pointer of some base class
    into a pointer of a derived class. Static_cast does not make any checks as
    dynamic_cast does. Static_cast if often used when either run-time type
    information are not available for the code or when you want to avoid the
    overhead of a dynamic_cast.

    As general rule of thumb one can say that the need for dynamic_casts means that
    your design is broken. I only use dynamic_casts in conjunction with Microsoft's
    MFC library (which suffers from numerous design flaws, but it was engineered
    quite a long time ago). If you use templates excessively (especially the
    Curiously Recurring Template pattern), you'll come to the point where you have
    to use static_casts quite often.

    Regards,
    Stuart
     
    Stuart Redmann, Nov 14, 2007
    #3
  4. Stuart Redmann wrote:
    > Static_cast is necessary when you want to down-cast a pointer of some
    > base class into a pointer of a derived class.


    That's not the only use for static_cast. It can also be legitimately
    used in things like: int i = static_cast<int>(aDouble);
     
    Juha Nieminen, Nov 14, 2007
    #4
  5. , India

    red floyd Guest

    Stuart Redmann wrote:

    > Static_cast is necessary when you want to down-cast a pointer of some
    > base class into a pointer of a derived class. Static_cast does not make
    > any checks as dynamic_cast does. Static_cast if often used when either
    > run-time type information are not available for the code or when you
    > want to avoid the overhead of a dynamic_cast.
    >


    Also for round-tripping to void*:

    T* pT;
    // ...
    void* pVoid = pT;
    // ...
    pT = static_cast<T*>(pVoid);

    Often used with APIs that require a C callback.
     
    red floyd, Nov 14, 2007
    #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. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    497
    Josephine Schafer
    Aug 4, 2003
  2. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    566
    Bill Seurer
    May 5, 2004
  3. Bo Peng
    Replies:
    11
    Views:
    1,112
    Victor Bazarov
    Oct 20, 2006
  4. Replies:
    4
    Views:
    442
    Alf P. Steinbach
    Feb 1, 2008
  5. junyangzou
    Replies:
    13
    Views:
    277
Loading...

Share This Page