Differences between dynamic_cast and static_cast

Source: Internet
Author: User

In C ++, dynamic_cast and static_cast are both operators used for transformation. Improper use of them may lead to a valid type conversion operation during the compilation period, but may cause errors during the runtime, errors are more likely to occur when the transformation operation involves object pointers or references. What are the differences between the two?

1. The dynamic_cast operator checks the security of suspicious transformation operations at runtime, while the static_cast operator does not;

2. dynamic_cast is only valid for polymorphism (the source type of transformation must be polymorphism, but it is irrelevant to whether the target type of transformation is polymorphism), while static_cast can be applied to any type;

3. dynamic_cast from the derived class to the base class can be performed, which is called upward transformation;

4. dynamic_cast from the base class to the derived class cannot be performed, which is called downward transformation;

5. There is an inheritance relationship. The derived class can be converted to the base class through dynamic_cast;

6. There is no inheritance relationship and cannot be exchanged through dynamic_cast;

Usage:

Dynamic_cast <T *> ptr, static_cast <T *> ptr;

Dynamic_cast <T &> p, static_cast <T *> p;

Some simple code is used below to illustrate some knowledge points about transformation:

A base class pointer can point to a base class object or a derived class object without explicit transformation operations. In turn, it is unwise for a derived class pointer to a base class object.

1 class B 2 {3... 4}; 5 class D: public B 6 {7... 8}; 9 int main () 10 {11 D * p; 12 p = new B (); // error13 p = static_cast <D *> (new B ()); // valid, but may cause difficult-to-Trace running errors 14}

Let's look at the following code:

 1 class B 2 {   3  public: 4      virtual void f() { cout<< "f()" <<endl; } 5 }; 6  class D : public B 7 { 8  public: 9      void m(){cout<< "m()" <<endl;}10 }; 11 int main()12 {13       D* p = static_cast<D*>(new B); 14       p -> m();   // ...15       return 0;    16 }

P-> m () can be compiled, but an error occurs. Because P actually points to a B object, and B does not have a member function m, this transformation is not safe (in VS2010, it can be run correctly, and the output is m ()).

The dynamic_cast operator provided by C ++ can detect whether a transformation action is safe at runtime. Dynamic_cast and static_cast have the same syntax, but dynamic_cast is only valid for Polymorphism types.

1 class C 2 {3/C class no virtual function 4}; 5 class T 6 {7}; 8 int main () 9 {10 T * p = dynamic_cast <T *> (new C (); // error, effective only for Polymorphism Type 11 return 0; 12}

Note: If the dynamic_cast operation is correct, the source type of the transformation must be polymorphism, but it is irrelevant to whether the target type of the transformation is polymorphism. The target type of dynamic_cast specified in <> must be a pointer or reference.

Let's take a look at the correct usage:

1 class B 2 {3 public: 4 virtual void f () {cout <"f ()" <endl ;}5}; 6 class D: public B 7 {8 public: 9 void m () {cout <"m ()" <endl ;}10}; 11 int main () 12 {13 D * p = dynamic_cast <D *> (new B (); // determines whether the transformation is secure. If it is secure, the address of object B is returned, otherwise, NULL is returned. In this example, NULL is returned. 14 if (p) 15 {16 p-> m (); 17} 18 else19 {20 cout <"Error \ n"; 21} 22 return 0; 23}

If there is any incorrect or incomplete welcome correction and supplement --

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.