Clause 2: it is best to use the C ++ transformation operator (1)

Source: Internet
Author: User

Think about low-level transformation. Similar to goto, it is regarded as a "daemon" in program design ". Even so, it can still survive, because when a certain situation gets worse, the transformation may be necessary. Yes. transformation is necessary when some situations get worse!

However, the old C transformation method is not the only choice. It almost allows you to convert any type to any other type, which is very poor. It would be better if the intention is more precise in each transformation. For example, convert a pointer-to-const-object to a pointer-to-non-const-object (that is, only changing the object's constants ), and transform a pointer-to-base-class-object into a pointer-to-derived-class-object (that is, completely changing the type of an object ), there are great differences. The traditional C transformation action is not differentiated (this should not surprise you, because the C transformation is designed for C, not for C ++ ).

The second problem with old-style transformations is that they are hard to recognize. The syntax structure of the old transformation is composed of a pair of parentheses with an object name (identifier), and the parentheses and object names may be used anywhere in C ++. Therefore, we cannot answer the most basic transformation-related questions. "Does this procedure use any transformation actions? ". Because people are likely to turn a blind eye to transformation actions, and tools such as grep cannot differentiate some non-transformation statements with extremely similar syntaxes.

To solve the disadvantages of C's legacy transformation, C ++ imports four new transformation operators (cast operators): static_cast, const_cast, dynamic_cast, and reinterpret_cast. For most purposes, the only thing you need to know about these operators is the usual form of code writing:

 
 
  1. (type) expression

Now we should change it to this:

 
 
  1. static_cast (expression)

For example, suppose you want to convert an int into a double and use an integer expression to export a floating point value. The old-style C transformation can be implemented as follows:

 
 
  1. int firstNumber, secondNumber; ...
  2. double result = ((double)firstNumber)/secondNumber;

If the new C ++ transformation method is adopted, it should be written as follows:

 
 
  1. double result = static_cast (firstNumber)/secondNumber;

This form is easily identified, either for humans or for tool programs.

Static_cast has the same power and significance as the old-style C transformation, as well as the same restrictions. For example, you cannot use static_cast to convert a struct to an int, or convert a double to a pointer. These are tasks that cannot be completed by the old C transformation. Static_cast cannot even remove constness of an expression because it has a new transformation operator const_cast.

Other new C ++ transformation operators apply to more concentrated (narrower scope) purposes. Const_cast is used to change constness or volatileness in an expression ). The use of const_cast emphasizes to humans (as well as compilers) that the only thing you intend to change through this transformation operator is the constant or variation of something. This will be implemented by the compiler. If you apply const_cast to other purposes than above, the transformation will be rejected. The following is an example:

 
 
  1. Class Widget {...}; class SpecialWidget: public Widget {...};
  2. Void update (SpecialWidget * psw); SpecialWidget sw; // sw is a non-const object,
  3. Const SpecialWidget & csw = sw; // csw is a reference representing sw. // It is regarded as a const object.
  4. Update (& csw); // error! Const SpecialWidget *
  5. // Pass it to a function that requires SpecialWidget.
  6. Update (const_cast (& Csw); // yes! & The constant of csw is removed. Therefore,
  7. // Csw (sw) can be changed in this function.
  8. Update (SpecialWidget *) & csw); // The same is true for the situation, but it is difficult to identify
  9. // C old transformation syntax.
  10. Widget * pw = new SpecialWidget; update (pw); // error! Pw is a Widget *,
  11. // Update () requires SpecialWidget *.
  12. Update (const_cast (Pw); // error! Const_cast can only be used to affect
  13. // Constant or variable, unable to carry out the downward Transformation (cast down) action of the inheritance system.

Obviously, the most common purpose of const_cast is to remove the constants of an object.

The second special transformation operator is dynamic_cast, which is used to perform "Safe downward or cross-system transformation actions" in the inheritance system ". That is to say, you can use dynamic_cast to convert "pointers or references pointing to base class objects" to "pointers or references pointing to derived (or sibling base) class objects ", and determine whether the transformation is successful. If the transformation fails, it will be expressed as a null pointer (when the transformation object is a pointer) or an exception (when the transformation object is a reference:

 
 
  1. Widget * pw ;...
  2. Update (dynamic_cast (Pw); // very good, pass to update () A pointer, pointing
  3. // SpecialWidget referred to by pw -- if pw // really points to something like this; otherwise, it passes
  4. // It will be a null pointer.
  5. Void updateViaRef (SpecialWidget & rsw );
  6. UpdateViaRef (dynamic_cast (* Pw); // very good. What is passed to updateViaRef () is
  7. // SpecialWidget referred to by pw -- If // pw really points to something like this; otherwise
  8. // Throw an exception.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.