当前位置:网站首页>C + + programming experience (6): using C + + style type conversion

C + + programming experience (6): using C + + style type conversion

2021-08-07 23:43:40 Look, the future

Why is it recommended to use C++ Style conversion ?

Not that the type conversion mechanism of other styles is not good , But write C++ code , Since they have , Then slowly adapt , When in Rome, do as the Romans do .

We used to write type conversions like this :(type) expression, and C++ Four type conversion operators are introduced :

static_cast 
const_cast
dynamic_cast
reinterpret_cast

   
  • 1.
  • 2.
  • 3.
  • 4.

Write like that before , Now it's just written like this :static_cast(expression)

For example :
Suppose you want to put a int convert to double, So that it contains int The expression of type variable produces the result of floating-point value .

If you use C Style conversion , You can write :

int a; 
... 
double b = (double)a; 

   
  • 1.
  • 2.
  • 3.

If you use the above new type conversion method , You should write like this :

double result = static_cast<double>(a); 

   
  • 1.

How to control C++ Style conversion ?

static_cast I won't say more , As mentioned earlier , What about function? , Follow C The type conversion function of style is basically the same .

But what? , It also has functional limitations . for example , You can't use static_cast Elephant use C The style of type conversion is the same as struct convert to int Type or put double Type to pointer type , in addition ,static_cast Cannot remove... From an expression const attribute , Because this is another type converter (const_cast) The function of .


const_cast Used for and only for expressions whose type is converted const or volatileness attribute .

class father { ... }; 
class son: public father { ... }; 
void update(son* psw); 
son sw;                // sw  It's a non  const  object . 
const son& csw = sw;   // csw  yes  sw  A reference to , It's a  const  object 

update(&csw);  //  error ! Cannot pass a  const son*  Variable to a process  son* Functions of type variables   
update(const_cast<son*>(&csw)); //  correct ,csw  Of  const  Be shown to convert  
update((son*)&csw);	//  ditto , But a more difficult to identify  C  Style conversion 
 
father *pw = new son;  
update(pw);         //  error !pw  The type is  father*, however  update  The function deals with  son* type   
update(const_cast<son*>(pw));//  error !const_cast  Can only be used to influence  constness or volatileness  Where we live ., 
                    //  Cannot be used for type conversion to an inherited subclass . 

   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

dynamic_cast, It is used to safely type down the inheritance relationship of the class . That is to say , You can use dynamic_cast Convert a pointer or reference to a base class to a pointer or reference to its derived class or its sibling class , And you can know if the conversion was successful . The failed conversion will return a null pointer ( When you type a pointer ) Or throw an exception ( When you type a reference ):

father* pw; 
... 
update(dynamic_cast<son*>(pw));  //  correct , Pass to  update  A pointer to a function points to a variable of type  son Of  pw  The pointer to  

void updateViaRef(son& rsw); 
updateViaRef(dynamic_cast<son&>(*pw));  // correct .  Pass to  updateViaRef  function  SpecialWidget pw  The pointer , If  pw  

   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

The last of the four type converters is reinterpret_cast.emmm, It seems that this converter is not very popular .

Use the type conversion of this operator , Almost all of the conversion results are execution time definitions . therefore , Use reinterpret_casts It's very difficult to port the code of .

reinterpret_casts The common use of is to convert between function pointer types .

The code for conversion function pointers is not portable (C++ It is not guaranteed that all function pointers are represented in the same way ), In some cases, such a transformation can produce incorrect results , So you should avoid converting function pointer types , Unless you have to .

 

版权声明
本文为[Look, the future]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210807234229190f.html

随机推荐