C ++ type conversion dynamic_cast Parsing

Source: Internet
Author: User

C ++ type conversion dynamic_cast Parsing

There are four types of C ++ conversions: const_cast, reinterpret_cast, static_cast, and dynamic cast.

Here is a small example of dynamic cast.


# Include
 
  
Using namespace std; class BaseClass {public: int m_iNum; virtual void foo () {}; // The base class must have virtual functions. Dynamic_cast}; class DerivedClass: public BaseClass {public: char * m_szName; DerivedClass (char * str) {m_szName = new char [strlen (str) + 1]; strcpy (m_szName, str );}~ DerivedClass () {if (m_szName! = NULL) {delete [] m_szName; m_szName = NULL ;}} void bar () {};}; int main () {// 64-bit system, int: 4, vtable_prt: the output value of 8 is 16 cout <"sizeof (BaseClass) =" <sizeof (BaseClass) <endl; // 64-bit system, 4 + 8 + 8 = 24 cout <"sizeof (DerivedClass) =" <sizeof (DerivedClass) <endl; char * p = "abc "; baseClass * pb = new DerivedClass (p); cout <"sizeof (* pb):" <sizeof (* pb) <endl; // 16 DerivedClass * pd1 = static_cast
  
   
(Pb); // subclass-> parent class, static type conversion, correct, but not recommended DerivedClass * pd2 = dynamic_cast
   
    
(Pb); // subclass-> parent class, dynamic type conversion, correct cout <pd1-> m_szName <endl; // abc cout <pd2-> m_szName <endl; // abc cout <"sizeof (* pd1):" <sizeof (* pb) <endl; // 16 cout <"sizeof (* pd2):" <sizeof (* pb) <endl; // 16 BaseClass * PBS = new BaseClass (); derivedClass * pd21 = static_cast
    
     
(PBS); // parent class-> subclass, static type conversion, dangerous! Access subclass m_szName member out of bounds DerivedClass * pd22 = dynamic_cast
     
      
(); // Parent class-> subclass, dynamic type conversion, secure. The result is NULL cout <"sizeof (* pd21):" <sizeof (* pb) <endl; // 16 cout <"sizeof (* pd22 ): "<sizeof (* pb) <endl; // 16 cout <pd21 <
      
        M_szName indicates segmentation fault cout <pd22 <
       
        
Output result:

sizeof(BaseClass) = 16sizeof(DerivedClass) = 24sizeof(*pb): 16abcabcsizeof(*pd1): 16sizeof(*pd2): 160x7f9a7b403a600sizeof(*pd21): 16sizeof(*pd22): 16



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.