The difference between C + + dynamic_cast and static_cast

Source: Internet
Author: User

Today in the Wang Daobao to see the dynamic_cast, has been useless, and do not understand, today to summarize.

Both dynamic_cast and static_cast can be used to cast pointer types, but the difference is that dynamic_cast is more secure when it comes to downstream conversions between class hierarchies .

The dynamic_cast operator can determine the true type at the execution time. If the downstream conversion is secure (that is, if the base class pointer or reference does point to a derived class object), the operator returns the transformed pointer. If the downstream conversion is unsafe, this operator will pass the Go home pointer (that is, the base class pointer or reference does not point to a derived class object).

What does it mean to say so much? The summary can be in two words:

1) The effect of dynamic_cast and static_cast is the same when upstream conversions are performed between class hierarchies.

2) dynamic_cast has the function of type checking, which is more secure than static_cast, when the downstream conversion is performed between class hierarchies.

Example: In the following code, What is the difference between Statement 1 and statement 2 if the argument calling the function func points to an object of type B?

1#include <iostream>2 using namespacestd;3 4 classb{5  Public:6B (): B (1){}7     Virtual voidfoo () {};8     intb;9 };Ten  One classD: Publicb{ A  Public: -D ():d (2){} -     intD; the }; -  - voidFunc (B *pb) { -D *PD1 = Static_cast<d *> (pb);//Statement 1 +cout<<pd1->b<<Endl; -cout<<pd1->d<<Endl; +      AD *pd2 = Dynamic_cast<d *> (pb);//Statement 2 atcout<<pd2->b<<Endl; -cout<<pd2->d<<Endl; - } -  - intMain () { -     //b* PB = new D; in     //func (PB); -b* PB =NewB; to func (PB); +     return 0; -}

First, in the code above, if PB points to an object of type D, i.e.

    New D;    Func (PB);    

Output:

The PD1,PD2 is the same, and the result is normal, and the two pointers perform any operation of type D is safe.

Second, however, if PB is pointing to a Type B object,

New B;    Func (PB);

Output:

Then PD1 will be a pointer to the B object, the operation of type D will be unsafe (such as access D), output D value, will be a garbage value (this output is 0), delayed the wrong discovery;

Instead, PD2 will be a null pointer to the null pointer, and an exception will occur, allowing an earlier error to be found.

In addition, it is important to note that using dynamic_cast must declare the virtual field, that is, to have a virtual function table! the error output is as follows:

This is because dynamic_cast run-time type checking requires type information, and this type of information is stored in the virtual function table of the class, and only the class that defines the virtual function has a virtual function table, so the compilation error. static_cast, however, has no such restriction .

These are the differences between dynamic_cast and static_cast.

The difference between C + + dynamic_cast and static_cast

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.