Forcing type conversions

Source: Internet
Author: User

Forced type conversions in C + +: static_cast, reinterpret_cast, Const_cast, Dynamic_cast.static_cast: For conversions between non-polymorphic types (static transformations), it can be used between any standard type, However, it cannot be used for conversions between unrelated types. Static_cast can only be used for conversions between related types. Example:       int i = 2;       Double d = static_cast<double> (i); Correct, correlation type conversion             int *p = &i;       Double d = static_cast<double> (P);//error, cannot convert, do not want to close type reinterpret_ Cast: The operator is used to convert one type to another different type. Example: typedef void (*FUNC) (), int dosomthing (int i) {      cout << " Dosomthing (): "<< i << Endl;       return 0;} void Test () {      FUNC pf = reinterpret_cast<func> (dosomthing);       PF ();} Reinterpret_cast: You can have the compiler look at the dosomething function in a way that is defined by Func, but this sometimes produces indeterminate results. In a word, reinterpret_cast is a very bug. Const_cast: The most common use is to remove the const attribute of a variable to make it easy to assign a value. Example: void Test () {      const int i = Ten,       int* b =const_cast<int *> (&i);       *B = 20;       cout << i << Endl;      //result is 10 because the compiler is optimized, so it will be valued in the register       cout << *b << Endl;    //The result is that 20,b points to I in memory, changing the value in memory}void test () {      volatile const int i = Ten;         int* b =const_cast<int *> (&i);       *b = 20;       cout << i << Endl;      //result is 20 because volatile guarantees memory visibility       cout << *b << Endl;    }dynamic_cast: A pointer or reference to convert a pointer or reference of a parent class object to a subclass object. 1. dynamic_cast can only be used for Class 2 that contains virtual functions, if the parent pointer is to a parent class object, 0 is returned, and if the parent pointer points to the subclass object, the conversion succeeds. Example: void Test () {      A A,       b b,       A *pa = &a;  //is correct, the parent pointer points to the parent Class object       PA = &b;     Correct, parent pointer pointing to subclass object      //b *PB = &a; &NBSP;//C Error, sub-class pointer to parent class object       B *PB = &b;      //is correct, the sub-class pointer points to the subclass object}void Fun (A *pa) {      B *PB = dynamic_cast<b *>(p a);        //if the PA points to the parent class object, returns 0 if it points to a subclass object, successfully converted       cout << "PB1:" << pb<< E NDL;} void Test () {      a A;       b b;       Fun (&a)       &N Bsp         cout << &a << Endl;       Fun (&b);       cout << &b<< Endl;} Explicit: Preventing implicit type conversions by constructors: Class A{public:       A (int a)             : _a (0) {}       A (const a& a)       {     }private:       int _a;}; void Test () {      a A1 (1);  //Direct initialization      //1 implicitly converted to a TMP (1), a A2 (TMP)     &N Bsp A A2 = 1;      //correct}class a{public:       Explicit A (int a)              :_a (0) {}       A (const a& a)     &NBSp {     }private:       int _a;}; void Test () {      a A1 (1);  //Direct initialization      //1 implicitly converted to a TMP (1), a A2 (TMP)     &N Bsp A A2 = 1;    //Error} If you do not want to generate intermediate objects, you can declare explicit before the constructor.

This article from the "11132019" blog, reproduced please contact the author!

Forcing type conversions

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.