[C ++ Basics] C ++ 4 transformation operators-data type conversion

Source: Internet
Author: User

[Cpp] // format: xxx_cast <type> (express) # pragma once # include <string> # include <iostream> # include <conio. h> using namespace std; // static_cast: basically has the same power and meaning as the old-style C transformation and the same restrictions. It is the most used, mainly in the conversion between basic types void test1 () {int first = 23, second = 31; double res = (double) first/second; // The old C syntax double res2 = static_cast <double> (first)/second; // cout <res <"" <res2 <endl; // 0.741935 0.741935 // char * str = "789 "; // cout <static_cast <int> (str) <endl; // error: cannot be converted from char * to int} // const_cast: it is used to remove the constness in the expression. The constant attributes are mostly reflected in pointers and references, because if there is no pointer or reference, there will be no data that cannot be modified accidentally. Void test2 () {int num = 123; const int * pNum = & num; // int * pNum1 = pNum; // errer: cannot convert from 'const int * 'to 'int *' int * pNum2 = (int *) pNum; // legacy C int * pNum3 = const_cast <int *> (pNum ); // new C ++ const_cast remove constants} // dynamic_cast: used to execute the "safe downward or cross-system transformation action" in the inheritance system. that is, the subclass Object Pointer is converted to the parent class Object Pointer. Dynamic_cast can be used to convert the pointer or reference pointing to the base classObject to the pointer or reference pointing to the derived classObject. If the transformation fails, a null pointer will be used (the pointer is converted) or an exception (if the conversion is reference) class B {public: virtual void fun () {cout <"B. fun () "<endl ;}}; class D1: public B {public: void fun () {cout <" D1.fun () "<endl ;} void fun2 () {cout <"D1.fun2 ()" <endl ;}}; class D2: public B {public: void fun () {cout <"D2.fun () "<endl;} void fun3 () {cout <" D2.fun3 () "<endl ;}}; void test3 () {// when the parent class pointer is converted to a subclass pointer, the parent class must have a virtual function B * pb = new D2 (); // pb-> fun (); // D2.fun () d1 * pd1 = dynamic_cast <D1 *> (pb); if (pd1) pd1-> fun2 (); // D2.fun () d2 * pd2 = dynamic_cast <D2 *> (pb); if (pd2) pd2-> fun3 (); // D2.fun3 () // convert the subclass pointer to the parent class pointer, no virtual function class CBase {}; // base class CDerived: public CBase {}; // inherits the class CDerived dc; CDerived * dp = & dc; CBase * PC3 = dp; // old-fashioned: The subclass pointer is converted to the parent class pointer, that is, the parent class Pointer Points to the subclass object CBase * cb1 = dynamic_cast <CBA Se *> (dp); // use dynamic_cast to convert the pointer pointing to the inherited class to the pointer printf ("point: % d, % d ", dp, PC3, cb1, & dc); // if their addresses are the same, they are in the same address in the memory. That is, the subclass pointer is converted to the parent class pointer, the parent class address points to the Child class address, which is the same. CBase & cc2 = dc; CBase & cb2 = dynamic_cast <CBase &> (dc ); // use dynamic_cast to convert the reference pointing to the inherited class to the reference pointing to the base class} // reinterpret_cast: The most common purpose is to convert the typedef void (* funcPtr) of the "function pointer" type) (); // funcPtr is a void function pointer type int iFunc () {return 0 ;} // iFunc is a void func (funcPtr f) {} // The void test4 () function pointer of funcPtr type () {// func (iFunc); // errer: parameter 1 cannot be converted from "int (_ cdecl *) (void)" to "funcPtr "; if the iFunc function is void iFunc () {};, func (reinter Pret_cast <funcPtr> (iFunc); // reinterpret_cast converts a function whose return value is int to a function whose return value is void. However, the input parameters must be the same} void test (char t) {cout <"press key =" <t <endl; switch (t) {case '1': test1 (); break; case '2': test2 (); break; case '3': test3 (); break; case '4 ': test4 (); break; case 27: case 'q': exit (0); break; default: cout <"default" <endl; break ;}} int main () {while (1) {test (getch ();} return 0 ;}

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.