C ++ four Explicit conversions (10)

Source: Internet
Author: User

1. reinterpret_cast: the result of implementation-dependent, which is specially used for underlying forced transformation,

For example, convert a pointer to an integer.

String;
Double * B = static_cast <double *> (& );
Cout <B <"" <& A <Endl; // The result is only a copy of the pointer to the pointer (address), and B is the same as the & A address.

There are also some defined objects, as long as there is a large data difference ..
Class {};
Class B {};
A * A = new;
B * B = reinterpret_cast <B *> ();
Cout <A <B <Endl; // The result address is the same.

Ii. static_cast: it can be used for forced implicit conversion (for example, converting a non-const object to a const object, or converting an int to a double object ), it can also be used for many reverse conversions (for example, void * pointer transformation to type pointer, base class pointer transformation to derived class pointer ), however, it cannot transform a const object to a non-const object (only const_cast can do it), and it is closest to the C-style conversion.
Class base {};
Class derived: public base {};
Base * A = new base;
Derived * B = reinterpret_cast <derived *> (a); (convert the subclass to the parent class, or vice versa .)
 // The above program can also be converted to the reinterpret_cast type, because it belongs to two different classes. The data types vary greatly.
 

In addition to the operation pointer, static_cast can also operate data, similar to the previous simple type conversion.

, Such
Double A = 1.111;
Int B = static_cast <int> ();

 

3. const_cast: It is generally used to force the elimination of object constants. It is the only forced transformation of the C ++ style that can do this.
Const int A = 10;
Int * B = const_cast <int *> (& );
* B = 9;
Cout <* B <Endl; // you can change the constant. 9

The constant pointer of the class is converted into a non-constant pointer.
Class C {};
Const C * A = new C;
C * B = const_cast <C *> ();

 

4. dynamic_cast: it is mainly used to perform "Safe downcasting" and safely convert the pointer or reference of the base class type to the pointer or reference of the derived type.
When used for polymorphism. Dynamic_cast checks whether the operation is valid. static_cast does not check whether the operation is executed at runtime.

1. If the converted pointer is not a valid and complete object pointer requested, the returned value is 0:
Class base {virtual void dummy (){}};
Class derived: public base {};
Base * baseptr = new derived;
Base * baseptr2 = new base;
If (derived * derivedptr = dynamic_cast <derived *> (baseptr) // In the second case, change baseptr to baseptr2;
{// Baseptr points at a derived object
Cout <"success" <Endl;
} Else {// baseptr points at a base object
Cout <"baseptr points at a base object" <Endl;
}
// The above program can use static_cast, but it is not detected. The second pointer is invalid ...,
// Reinterprep_cast can be used, and no detection is performed. We do not recommend that you...
After analyzing the above program, baseptr creates a derived class space at the beginning. If the space is consistent during the forced conversion process, success is output.
Baseptr2 creates a base space at the beginning, and then forcibly transforms the time-space inconsistency, and outputs 2nd statements.

2. If a reference type performs type conversion and this conversion is not possible, a bad_cast exception type is thrown, as shown below:
Derived D1;
Base temp;
Base & b1 = D1;
Base & b2 = temp;
Try
{
Derived D2 = dynamic_cast <derived &> (B1); // In the second case, change B1 to B2.
Cout <"success" <Endl;
}
Catch (bad_cast)
{
Cout <"exception..." <Endl;
}
The reference function is similar to the pointer function...

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.