C ++ casting operators and traditional C casting operators

Possible duplicate:
When should static_cast, dynamic_cast, and reinterpret_cast be used?

I searched the search engine many times:

  • why use C ++ casting operators over traditional C-style casting operators?
  • When to use C ++ casting operators, some live examples?

Below I found the following:

  • Traditionally, any C ++ casting operators are used to better serve your code (that is), we can easily find where casting is used in the code by simply searching for this complex notation (reinterpret_cast <), unlike C-style cast operators.

Now let me briefly outline why and when for each of the C ++ casting operators

static_cast

Why use it in C style? static_cast used to convert between related types.

Examples:

  Class A {}; Class B {}; A* a = new A(); B* b = static_cast<B*>(a); // Compiler error B* b1 = (A*)a; // Works fine float f; int addr = (int)(&f); // Works fine int addr = static_cast<int>(&f); // Compiler error 

But I wanted to know a real example of using the code above?

reinterpret_cast:

reinterpret_cast distinguishes pointers to unrelated types.

Examples:

  Class A {}; Class B {}; A* a = new A(); B* b = reinterpret_cast<B*>(a); // Works fine B* b1 = (A*)a; // Works fine float f; int addr = (int)(&f); // Works fine int addr = reinterpret_cast<int>(&f); // Works fine int ai = 10; float af = 13.33; // Would depend on how floating point is stored in machine // int& since reinterpret_cast expects either the type or operand to be pointer or reference int ki = reinterpret_cast<int&>(af); // ki would not be 13 int kitemp = (int)af; // kitemp would be 13 // The same reinterpret_cast behaviour can be achieved using this, int* in = (int*)(af); cout << (*in); 

My question is, how is reinterpret_cast different from C style customization? I cannot find why to use it over traditional casting operators, and also when to use it?

Another important example that makes these statements worse:

  const unsigned int * p; (int*)p; // Would remove unsigned and const at one shot // Using C++ casting operators // Const_cast expects a pointer or a reference reinterpret_cast<int*>(const_cast<unsigned int* >(p)); 

Writing the above code to remove const and unsigned much more complicated in C ++ listing? Then why do people use reinterpret_cast , const_cast or static_cast compared to traditional C casting operators?

I really understand the dynamic_cast used in the case of polymorphic classes; again, this operator also has the added cost of RTTI.

+4
source share
3 answers

The Google C ++ Style Guide provides some motivation for using C ++ style styles:

The problem with C-casts is the ambiguity of the operation; sometimes you do a conversion (like (int)3.5 ), and sometimes you do a cast (like (int)"hello" ); C ++ casts avoids this. Additionally When you search in C ++, notes become more visible.

I like C ++ roles because they do what you intend to make very explicit, letting the compiler catch the wrong use.

For example, if you know that you intend to perform only a numerical conversion to an integer, static_cast will only compile when this numerical conversion makes sense. As you showed in your code example, casting a C style will cast regardless of reality.

C ++ references are really intended for better documentation of intentions and to protect time from being compromised against unintentional use.

+7
source

It’s bad practice to remove the const specifier. You will probably end up writing a variable or memory area that you should not write to. So this part of your question is invalid.

0
source

From my memory, reinterpret_cast is almost the same as c style cast, except that I think that if you have const Thing, you cannot reinterpret_cast for noncost_other_thing (c stye allows you to remove them, which may not be intentional and probably dangerous).

I only use casting in my projects, because I have the luxury of being lazy, and sometimes I'm not lazy. You believe that using C ++ style and other C ++ features (ostream, not a file, no printfs, avoid memset and other unsafe functions, etc.) When using C ++. But most people just do whatever they want (and get errors because of this).

Usually, if you know when to use dynamic_cast and static cast, you'll be fine. I find reinterpret_cast likely if I don’t interact with C and you need to work with void *. const_cast ... I actually never use and hope I never need to. And you should "use them always."

PS: An unrelated note. I actually throw an exception and state (0) about unrealized things. If I do not process the parameter and expect it to be 0, I will write an exception or claim to test this. When I debug / add more code, I run them instead of errors, and theres absolutely no secret about why this happened :)

0
source

Source: https://habr.com/ru/post/1387988/


All Articles