Multi-inheritance pointer Conversion

Source: Internet
Author: User

Multi-inheritance pointer Conversion

The conversion of multiple inherited pointers has a great relationship with the data layout in the memory. Currently, msvc11 and gcc4.8.3 are used to test the result after pointer conversion. It is found that the address of the derived class is mostly the same as the first parent class address of the inherited list as long as it does not involve compilation and adding a virtual table pointer. Of course there will be other problems that affect the pointer conversion of the layout. The content to be mentioned here is: Do not use c_style pointer conversion. The correct pointer conversion is from dynamic_cast/static_cast.

The pointer object generated by the compiler has a reflection mechanism that maps the derived class address correctly from the parent class pointer. Whether it is normal inheritance or virtual inheritance. I still need to read the book about the specific memory layout to be continued.

The implementation of the compiler is too knowledgeable and affects our use of this language. To make a difference in the gaming engine in the next few decades, you must thoroughly understand C ++/C, mathematics, and computer graphics applications.




#include 
 
  #include 
  
   #include 
   
    #include 
    
     class A{public:A(){i = 10;};~A(){};virtual void display(){}int i;};class B{public:virtual void display(){}int j;};class C :public A,public B{public:int i;int k;private:};class D:virtual public A{public:int di;virtual void display() override{}private:};class  E :virtual public A{public:int ei;virtual void display() override{}private:};class F : public D,public E{public:int fi;virtual void display() override{}private:};int main(int argv, char **argc){C * pC = new C();B * pB = dynamic_cast
     
      (pC);A * pA = dynamic_cast(pC);B * pB_s = static_cast
      
       (pC);A * pA_s = static_cast(pC);C * pCB_D = dynamic_cast
       
        (pB);C * pCA_D = dynamic_cast
        
         (pA);C * pCB_S = static_cast
         
          (pB);C * pCA_S = static_cast
          
           (pA);pC->i = 100;printf("%d\n",pA->i);printf("pC = %d,pB_d = %d,pA_d = %d,pB_s = %d,pA_s = %d\n", pC, pB, pA, pB_s, pA_s);printf("pCB_D = %d,pCA_D = %d,pCB_S = %d,pCA_S = %d\n", pCB_D, pCA_D, pCB_S, pCA_S);F * pF = new F();D * pD = dynamic_cast
           
            (pF);E * pE = dynamic_cast
            
             (pF);D * pD_s = static_cast
             
              (pF);E * pE_s = static_cast
              
               (pF);F * pFD_D = dynamic_cast
               
                (pD);F * pFE_D = dynamic_cast
                
                 (pE);F * pFD_S = static_cast
                 
                  (pD);F * pFE_S = static_cast
                  
                   (pE);printf("pF = %d,pD_d = %d,pE_d = %d,pD_s = %d,pE_s = %d\n", pF, pD, pE, pD_s, pE_s);printf("pFD_D = %d, pFE_D = %d,pFD_S = %d, pFE_S = %d\n", pFD_D, pFE_D, pFD_S, pFE_S);return 0;}
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 


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.