Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:30:18+00:00 2026-05-24T04:30:18+00:00

I’ve got a question about how a C++ compiler knows how much space it

  • 0

I’ve got a question about how a C++ compiler knows how much space it needs to allocate for an object when using inheritance.

Imagine the following classes, for an adult and a child which extend a Person class, but where a child has a short book with them.

class Person
{

};

class Adult : public Person
{
    //No book
};

class Child : public Person
{
   char book[1000]; //Stores the book
};

Now if you create an array of Person objects, and add objects to them:

Adult adult1;
Child child1;

Person people[2];
people[0] = child1;
people[1] = adult1;

My question is:
How does the compiler know how much space it needs to ensure the array is a contiguous memory block, if it doesn’t know whether the array will be filled with Adult or Child (which are much different sizes) objects?

I hope this makes enough sense to answer…

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:30:19+00:00Added an answer on May 24, 2026 at 4:30 am

    How does the compiler know how much space it needs to ensure the array is a contiguous memory block, if it doesn’t know whether the array will be filled with Adult or Child

    The people array cannot contain Adult or Child objects: it can only contain Person objects.

    When you perform the assignment people[0] = child1;, what actually happens is the Person parts of the child1 object are assigned to people[0]. All of the Child parts are effectively ignored.

    This behavior, where only the base class parts of an object are copied, is called “slicing.”

    To get polymorphic behavior in C++, you must use either pointers are references. Since it is impossible to have an array of references, you would need to use an array of pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
i got an object with contents of html markup in it, for example: string
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.