C ++ derived class Object Memory Layout

Source: Internet
Author: User

  • Question:

Class C inherits from the base class A and base class B. Now there is an object of class C which converts its address to the base class A pointer and the base class B pointer respectively, are the values of these two pointers equal?

  • Answer:
Not equal.
  • Explanation:
As you can see, the derived class Object puts the information about each base class together in the memory and stores its own members. The values of class C object addresses after being converted to Class A and Class B pointers are shown in.
  • The following is a verification program:
// Cpp_learning.cpp: defines the entry point for the console application. <br/> // </P> <p> # include <iostream> <br/> using namespace STD; <br/> Class A <br/>{< br/> Public: <br/> int A; <br/> int B; <br/> virtual void message () <br/>{< br/> cout <"base a" <Endl; <br/>}< br/> }; </P> <p> Class B <br/> {<br/> Public: <br/> int C; <br/> int D; <br/> virtual void message () <br/>{< br/> cout <"Base B" <E NDL; <br/>}</P> <p >}; </P> <p> Class C: Public, public B <br/>{< br/> Public: <br/> int e; <br/> virtual void message () <br/>{< br/> cout <"derive C" <Endl; <br/>}< br/> }; </P> <p> void Casta (A) <br/> {<br/> cout <". A = "<. A <Endl; <br/> cout <". B = "<. B <Endl; <br/>. message (); </P> <p >}</P> <p> void castb (B) <br/>{< br/> cout <"B. C = "<B. c <Endl; <br/> cout <"B. D = "<B. d <Endl; <Br/> B. message (); <br/>}</P> <p> void castaref (A & A) <br/>{< br/> cout <". A = "<. A <Endl; <br/> cout <". B = "<. B <Endl; <br/>. message (); </P> <p >}</P> <p> void castbref (B & B) <br/> {<br/> cout <"B. C = "<B. c <Endl; <br/> cout <"B. D = "<B. d <Endl; <br/> B. message (); <br/>}</P> <p> int main () <br/>{< br/> C; <br/> C. a = 1; <br/> C. B = 2; <br/> C. C = 3; <br/> C. D = 4; <br/> C. E = 5; <Br/> C C1; <br/> A * pA = (A *) & C; <br/> B * pb = (B *) & C; <br/> A & A = C; <br/> B & B = C; </P> <p> If (INT) pA = (INT) Pb) <br/>{< br/> cout <"Pa = Pb" <Endl; <br/>}< br/> else <br/>{< br/> cout <"Pa! = Pb "<Endl; <br/>}</P> <p> If (INT) & A = (INT) & B) <br/>{ <br/> cout <"& A = & B" <Endl; <br/>}< br/> else <br/>{< br/> cout <"&! = & B "<Endl; <br/>}</P> <p> If (INT) & A = (INT) Pa) <br/>{< br/> cout <"& A = PA" <Endl; <br/>}< br/> else <br/>{< br/> cout <"&! = PA "<Endl; <br/>}</P> <p> If (INT) & B = (INT) Pb) <br/>{< br/> cout <"& B = Pb" <Endl; <br/>}< br/> else <br/>{< br/> cout <"& B! = Pb "<Endl; <br/>}</P> <p> Casta (c); <br/> castb (C ); <br/> castaref (c); <br/> castbref (c); <br/> cout <"Pa =" <(INT) Pa <Endl; <br/> cout <"PB =" <(INT) Pb <Endl; <br/> cout <"& C. E = "<(INT) & C. E <Endl; <br/> cout <"sizeof A =" <sizeof (a) <Endl; <br/> cout <"sizeof B =" <sizeof (B) <Endl; <br/> cout <"sizeof c =" <sizeof (c) <Endl; </P> <p> return 0; <br/>}
  • Execution result:
Pa! = Pb
&! = & B
& A = pa
& B = Pb
A. A = 1
A. B = 2
Base
B. C = 3
B. D = 4
Base B
A. A = 1
A. B = 2
Derive C
B. C = 3
B. D = 4
Derive C
Pa = 2293536
Petabytes = 2293548
& Amp; C. E = 2293560
Sizeof A = 12
Sizeof B = 12
Sizeof c = 28

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.