The type conversion of C++ is described in detail

  • 2020-05-24 05:56:22
  • OfStack

The type conversion of C++ is described in detail

1. Type conversion name and syntax

C style cast (Type Cast) is simple. Any cast is:

TYPE b = (TYPE)a

C++ style type conversion provides four types of conversion operators for different applications.

static_cast static type conversion. For example, int is converted to char

reinterpreter_cast reinterpreted type

dynamic_cast is technically understood as dynamic type conversion. Such as polymorphic type conversion between subclasses and superclasses.

const_cast literally means go to the const property.

Four types of conversion formats:

TYPE B = static_cast < TYPE > (a)

2, type conversion 1 general introduction

4. Introduction to type transformation

1) static_cast < > () static type conversion, c++ compiler will do type checking when compiling;

Basic types can be converted but pointer types cannot be converted

2) if there is a mandatory type conversion between different types, reinterpret_cast is used < > () to reinterpret

3) dynamic_cast < > (), dynamic type conversion, safe conversion between base and subclasses; Runtime type checking (C++ unique)

4) const_cast < > (), remove the read-only property of the variable (C++ unique). The type of the variable must be a pointer, and the memory space pointed to by the pointer can be modified

1 general conclusion

An implicit type conversion in the C language is available in c++ with static_cast < > () do the type conversion. Because the C++ compiler can pass the compile check 1;

If the C language does not allow implicit type conversion, reinterpret_cast can be used in c++ < > () perform forced type interpretation.

static_cast < > () and reinterpret_cast < > () basically casts the C language to override

reinterpret_cast < > () portability is difficult to guarantee.

3. Typical cases

The code contains examples of the four types of conversions, as well as points to note.


#include<iostream>
using namespace std;

class Animal
{
public:
 virtual void action()
 {
 cout<<"the action is animal's "<<endl;
 }
};

class Dog:public Animal
{
public:
 virtual void action()
 {
 cout<<"the action is dog's "<<endl;
 }

 void doSwim()
 {
 cout<<"the dog is swimming..."<<endl;
 }
};

class Cat:public Animal
{
public:
 virtual void action()
 {
 cout<<"the action is cat's "<<endl;
 }

 void doTree()
 {
 cout<<"the cat is claming tree..."<<endl;
 }
};

class Desk
{
public:
 void action()
 {
 cout<<"this is Desk, not belong Animal"<<endl;
 }
};

void ObjPlay(Animal *animl)
{
 animl->action();
 Dog *dog = dynamic_cast<Dog *>(animl);
 if(dog!=NULL) // Judge whether or not dog
 {
 dog->action();
 dog->doSwim();
 }

 Cat *cat = dynamic_cast<Cat *>(animl);
 if(cat!=NULL) // Judge whether or not cat
 {
 cat->action();
 cat->doTree();
 }
 cout<<"func ObjPlay is exit!!!\n"<<endl;
}

// Typical usage   Remove the read-only attribute of the parameter 
void Opbuf(const char *p)
{
 cout << p << endl;
 //char *p2 = p; err:const char * Can't initialize to char *
 //p[0] = 'b'; err: Must be a modifiable lvalue 
 char *p2 = const_cast<char*>(p); // Remove the read - only sign 
 p2[0] = 'b';
 cout << p << endl;
}

int main()
{
 // Static type conversion  static_cast<>()
 double d = 3.14159;
 int i1,i2;
 i1 = d; //C Implicit type conversion in 
 i2 = static_cast<int>(d); //C++ Static type conversion in 
 cout<<"C Medium type conversion: "<<i1<<endl;
 cout<<"C++ Medium type conversion: "<<i2<<endl;


 // Reinterpretation type reinterpret_cast<>()
 char *p = "abcd";
 int *p1 = NULL;
 int *p2 = NULL;
 p1 = (int *)p; //C Force type conversion in 
 //p2 = static_cast<int *>(p);   Compiler error, type conversion error, static type cannot convert pointer 
 p2 = reinterpret_cast<int *>(p); //C++ Reinterpreted types in 
 cout<<"C Intermediate type conversion "<<hex<<*p1<<endl;
 cout<<"C++ Medium type conversion: "<<hex<<*p2<<endl;

 // Dynamic type conversion  dynamic_cast<>()
 Animal an;
 Animal *pAn = &an;
 ObjPlay(pAn);

 Dog dog;
 Dog *pDog = &dog;
 ObjPlay(pDog);

 Cat cat;
 Cat *pCat = &cat;
 ObjPlay(pCat);

 Desk desk;
 Desk *pDesk = &desk;
 //Animal *pAn = dynamic_cast<Animal*>(pDesk);  Different base class Pointers cannot be converted to each other, safe 

 // Remove the read-only property of the variable, const_cast<>(), This type must be a pointer 
 char buf[100] = "aaaaaaaaaaaa";
 //Opbuf(buf);
 // Make sure that the memory space performed by the pointer can be modified   If it can't be modified   It will still cause a program exception 
 //Opbuf("dddddddddddsssssssssssssss");

 system("pause");
 return 0;
}
 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: