Why use static_cast(x) instead of (int)x?

前端 未结 9 1760
滥情空心
滥情空心 2020-11-22 02:38

I\'ve heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?

相关标签:
9条回答
  • 2020-11-22 02:42

    static_cast means that you can't accidentally const_cast or reinterpret_cast, which is a good thing.

    0 讨论(0)
  • 2020-11-22 02:45

    The question is bigger than just using wither static_cast or C style casting because there are different things that happen when using C style casts. The C++ casting operators are intended to make these operations more explicit.

    On the surface static_cast and C style casts appear to the same thing, for example when casting one value to another:

    int i;
    double d = (double)i;                  //C-style cast
    double d2 = static_cast<double>( i );  //C++ cast
    

    Both of these cast the integer value to a double. However when working with pointers things get more complicated. some examples:

    class A {};
    class B : public A {};
    
    A* a = new B;
    B* b = (B*)a;                                  //(1) what is this supposed to do?
    
    char* c = (char*)new int( 5 );                 //(2) that weird?
    char* c1 = static_cast<char*>( new int( 5 ) ); //(3) compile time error
    

    In this example (1) maybe OK because the object pointed to by A is really an instance of B. But what if you don't know at that point in code what a actually points to? (2) maybe perfectly legal(you only want to look at one byte of the integer), but it could also be a mistake in which case an error would be nice, like (3). The C++ casting operators are intended to expose these issues in the code by providing compile-time or run-time errors when possible.

    So, for strict "value casting" you can use static_cast. If you want run-time polymorphic casting of pointers use dynamic_cast. If you really want to forget about types, you can use reintrepret_cast. And to just throw const out the window there is const_cast.

    They just make the code more explicit so that it looks like you know what you were doing.

    0 讨论(0)
  • 2020-11-22 02:45

    static_cast, aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:

    double d = 3.14159265;
    int    i = static_cast<int>(d);
    
    0 讨论(0)
  • 2020-11-22 02:52

    In short:

    1. static_cast<>() gives you a compile time checking ability, C-Style cast doesn't.
    2. static_cast<>() can be spotted easily anywhere inside a C++ source code; in contrast, C_Style cast is harder to spot.
    3. Intentions are conveyed much better using C++ casts.

    More Explanation:

    The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.

    char c = 10;       // 1 byte
    int *p = (int*)&c; // 4 bytes
    

    Since this results in a 4-byte pointer pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

    *p = 5; // run-time error: stack corruption
    

    In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

    int *q = static_cast<int*>(&c); // compile-time error
    

    Read more on:
    What is the difference between static_cast<> and C style casting
    and
    Regular cast vs. static_cast vs. dynamic_cast

    0 讨论(0)
  • 2020-11-22 02:53

    One pragmatic tip: you can search easily for the static_cast keyword in your source code if you plan to tidy up the project.

    0 讨论(0)
  • 2020-11-22 02:54

    It's about how much type-safety you want to impose.

    When you write (bar) foo (which is equivalent to reinterpret_cast<bar> foo if you haven't provided a type conversion operator) you are telling the compiler to ignore type safety, and just do as it's told.

    When you write static_cast<bar> foo you are asking the compiler to at least check that the type conversion makes sense and, for integral types, to insert some conversion code.


    EDIT 2014-02-26

    I wrote this answer more than 5 years ago, and I got it wrong. (See comments.) But it still gets upvotes!

    0 讨论(0)
提交回复
热议问题