C ++ forced type conversion and forced type conversion

Source: Internet
Author: User

C ++ forced type conversion and forced type conversion
Static_cast can be used for any type conversion with a clearly defined definition, as long as it does not contain the underlying const. For example: int I = 3, j = 2; double slope = static_cast <double> (I)/j; static_cast can also be used to convert void * to another type of pointer: void * p = & slope; double * dp = static_cast <double *> (p); it can be considered that the forced type conversion in static_cast and C is similar, for example, the forced type conversion in C is as follows: double slope = (double) I)/j; static_cast can also be used to process the explicit constructor. Example: class A {public: explicit A (int n): n (n) {} int getN () {return n ;}private: int n ;}; int getN (A a) {return. in this case, getN (12); is invalid. This can be written as follows: getN (static_cast <A> (12); const_cast can only be used to change the underlying const. For example, convert a constant object to a non-constant object (cast away the const ). For example: const char * pc; char * p = const_cast <char *> (pc); of course, if the object to which the pc points is a constant, the above Code can also be compiled, however, the execution result is undefined. Note: Only const_cast can change the constant attribute of an expression. Changing the constant attribute of an expression in other ways will cause a compilation error. Const_cast is often used in function overloading. For example, a function accepts const-type references as parameters and returns a const-type reference: const string & shorter (const string & s1, const string & s2) {return s1.size () <= s2.size ()? S1: s2;} if we pass two very large string type variables as real parameters to this function, the return value is still const string &. So we need to reload this function. When its real parameter is not a constant, a very large reference is returned: string & shorter (string & s1, string & s2) {return s1.size () <= s2.size ()? S1: s2;} solves the problem. However, to avoid repeated code in the two overloaded functions, you can change the second function to string & shorter (string & s1, string & s2) {auto & r = shorter (const_cast <const string &> (s1), const_cast <const string &> (s2); return const_cast <string &> (r );} note: 1. the first row of the function body forcibly converts arguments to const references. Therefore, the return value r of shorter in the first row is bound to string. Therefore, it is safe to forcibly convert the second row of the function body into a string. 2. Use a non-const version function to call a function of the const version. Reinterpret_cast is converted at the memory bit level for various hors. For example, char * p; int n = reinterpret_cast <int> (p); dynamic_cast is used for dynamic type conversion in a derived system. Generally, the pointer (or reference) to the base class is converted to the pointer (or reference) to the derived class. When the conversion fails (that is, the pointer (or reference) to be converted) it is not a pointer (or reference) to a derived class. 1. The conversion object is a pointer. A pointer with a value of 0 is returned. 2. The conversion object is a reference. An exception std: bad_cast is thrown, this exception is defined in the header file of the typeinfo standard library. Note: dynamic_cast is slower than other types in most compilers. Note: to convert a non-const variable to a const, you can use static_cast to convert a const to a non-const. You can only use const_cast. It is reasonable to use const_cast In the overload. In addition, the mandatory type conversion (static_cast and dynamic_cast) must be used with caution. Reinterpret_cast is especially used for nonsense.

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.