Exercise tive C ++'s book note item27: do as little transformation as possible

Source: Internet
Author: User

1. c ++ is a strongly typed language. Generally, the compiler checks out unsuitable transformation operations.

2. type conversion format:

  • Old-style transformation (the two methods are essentially the same ):

    • C style: (t) expression.
    • Function style: T (expression ).
  • New Style (or C ++ style ):
    • Const_cast <t> (expression): removes the constants of expressions. It is the only transformation operator in C ++ that can perform this operation.
    • Dynamic_cast <t> (expression): Mainly used to perform "secure downward transformation", that is, to determine whether an object belongs to a type in the inheritance system. It is the only action that cannot be executed by the old syntax and the only transformation action that may consume significant operation costs.
    • Reinterpret_cast <t> (expression): attempts to perform a low-level transformation (for example, converting a pointer to an integer variable to an integer). The actual action and result may be related to the compiler, this reduces portability.
    • Static_cast <t> (expression): implicit conversion. Note that the non-const type can be converted to const, but not vice versa.

If the transformation is necessary, try to use new styles, which are easier to identify and be corrected by the compiler.

3. if a class has one or more constructors with a single parameter, this class supports implicit type conversion: if necessary, it can automatically convert an object that accepts this parameter type to this type.

However, unless you want to define implicit conversions for obvious reasons, the constructor of a single parameter should be declared as explicit (add the explicit keyword before the function declaration, but do not add this keyword to the definition outside the class ). In this way, the type conversion must be explicitly called.

In the p118 example, explicit type conversion is not required. It is best to construct an object.

4. point A base class pointer to the object of the derived class. If the base class is not a virtual base class, implicit type conversion will occur (in the p118 example): the object is converted from the derived class to the base class, this causes "address offset ". Since each compiler has different la s of memory processing, if this la s are used in the program, the portability may be reduced.

5. Many application frameworks require virtual functions in the derived class to call functions of the same name in the base class first, for example:

Class window {public: Virtual void onresize (){...}...};... class specialwindow: public window {public: Virtual void onresize () {static_cast <WINDOW> (* This ). onresize (); // tries to convert "Current object" to an object of the base class type and call the function of the same name of the base class... // This function is different from the specific behavior of functions with the same name as the base class }...};

The above call is not to call the function of the current object, butThe onresize () function on the temporary copy of the "* This object's base class section" created by the transformation operation earlierIt first calls the onresize () function of the base class on the copy of the "base class composition of the current object, then, the specific behavior on the current object is different from that of the base class function. Therefore, if the onresize () function of the base class changes the value of the member variable, its execution in the derived class is invalid.

The corresponding code should be modified:

Class specialwindow: public window {public: Virtual void onresize () {window: onresize (); // call window: onresize () acting on * This ...}...};

6. dynamic_cast is slow and should be avoided. Consider the following situation: to execute the operation function of the derived class on the object of the derived class, but there is only one pointer or reference to the base class, you can avoid it in two ways:

(1) Use a container and store the pointer directly pointing to the object of the derived class in it. (PS: it may mean that the original base class pointer is no longer needed, so you can build your own smart pointer. Er, why do you think the author is bored ......)

(2) provide virtual functions in the base class and use polymorphism. (PS: this is a great deal of stuff. I didn't expect it to come here .)

7. In short, transformation should always be avoided. When it is unavoidable, transformation operations should be isolated in functions to facilitate maintenance.

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.