[C ++] type conversion

Source: Internet
Author: User

[C ++] type conversion

  • Introduction

The C ++ style has four conversion methods: static_cast, dynamic_cast, reinterpret_cast, and const_cast.

Welcome to lovickie's blog http://www.cnblogs.com/lovickie

  • Static_cast

Static_cast can be used for static conversions close to the C-style, but security considerations are added.

double d = 3.14;int i = static_cast<int> (d);

Welcome to lovickie's blog http://www.cnblogs.com/lovickie

  • Dynamic_cast

Uplink conversion (subclass pointer to parent class pointer) is generally safe. Of course, only public inheritance is required. Downlink conversion (parent class pointer and rotor class pointer) is likely to be insecure (for example, when the Pointer Points to the parent class object ).

The condition that dynamic_cast <type> (expression) can be used for dynamic conversion is: type is a pointer or reference, and the base class is polymorphism (with virtual functions, this allows you to place a type information pointer in a virtual table, a typical Implementation of dynamic_cast ). Dynamic_cast returns NULL for insecure downstream pointer conversions.

Class Dad {}; class Son: public Dad {public: void func () ;}; int main () {Dad d; Son * s1 = (Son *) & d; // compile through s1-> func (); // run the error Son * s2 = static_cast <Son *> (& d); // compile the error: unable to convert s2-> func (); Son * s3 = dynamic_cast <Son *> (& d); // compilation error: Dad is not a polymorphism type, unless there is a virtual function in Dad}
Class Dad {virtual void func () ;}; class Son: public Dad {}; int main () {Son son; dad * pd = dynamic_cast <Dad *> (& son); // uplink conversion, OK Dad dad; Son * ps = dynamic_cast <Son *> (& dad ); // downstream conversion, ps = 0 Dad * pdad = new Dad, * pson = new Son; ps = dynamic_cast <Son *> (pdad); // downstream conversion, ps = 0 ps = dynamic_cast <Son *> (pson); // downstream conversion, OK}

As shown in the preceding example, if the conversion fails, dynamic_cast returns 0. If the conversion fails, a bad_cast exception is thrown.

Void f (Dad & dad) {try {Son & son = dynamic_cast <Son &> (dad);} catch (bad_cast) {// Exception Handling }}

When the conversion source pointer or target pointer is void *, dynamic_cast always considers it safe. The target pointer is void *, which can be used to determine the starting address of an object of the polymorphism type.

Class A {virtual void func () ;}; int main () {A * a = new A; void * v = dynamic_cast <void *> (); // A polymorphism a = dynamic_cast <A *> (v );}

Like typeid, dynamic_cast uses "runtime Type Information" RTTI (Run Time Type Information), which may not be supported by some platforms.

Welcome to lovickie's blog http://www.cnblogs.com/lovickie

  • Reinterpret_cast

You can use reinterpret_cast <type> (expression) to convert the type between pointers or between pointers and integers. The type must also be a pointer or reference.

Int * p = new int; long I = reinterpret_cast <long> (p); p = 0; // p no longer points to new intp = reinterpret_cast <int *> I; // p points to new int again

Welcome to lovickie's blog http://www.cnblogs.com/lovickie

  • Const_cast

You can use const_cast to convert constants into variables and remove the const attribute.

const char *str = "hello";char *s = const_cast<char*> (str);

In the const member function of Class A, if you want to modify the member variable of Class A, you can force this to remove the const attribute. But when the object itself is a constant, the results will be unpredictable.

Class A {bool m; public: void func () const {A * a = const_cast <A *> (this ); // forcibly remove the const attribute a-> m = true ;}}; int main () {A a1; const A a2; a1.func (); // OK a2.func (); // no defined behavior}

Therefore, a better solution for modifying certain member variables by the const member function is to declare member variables as mutable or encapsulate these member variables into struct pointers.

Welcome to lovickie's blog http://www.cnblogs.com/lovickie

  • Conclusion

None.

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.