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

  • Home
  • SEARCH
  • 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 732557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:09:21+00:00 2026-05-14T07:09:21+00:00

Suppose I have class A { public: void print(){cout<<A; }}; class B: public A

  • 0

Suppose I have

class A           { public: void print(){cout<<"A"; }};
class B: public A { public: void print(){cout<<"B"; }};
class C: public A {                                  };

How is inheritance implemented at the memory level?

Does C copy print() code to itself or does it have a pointer to the it that points somewhere in A part of the code?

How does the same thing happen when we override the previous definition, for example in B (at the memory level)?

  • 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-14T07:09:21+00:00Added an answer on May 14, 2026 at 7:09 am

    Compilers are allowed to implement this however they choose. But they generally follow CFront’s old implementation.

    For classes/objects without inheritance

    Consider:

    #include <iostream>
    
    class A {
        void foo()
        {
            std::cout << "foo\n";
        }
    
        static int bar()
        {
            return 42;
        }
    };
    
    A a;
    a.foo();
    A::bar();
    

    The compiler changes those last three lines into something similar to:

    struct A a = <compiler-generated constructor>;
    A_foo(a); // the "a" parameter is the "this" pointer, there are not objects as far as
              // assembly code is concerned, instead member functions (i.e., methods) are
              // simply functions that take a hidden this pointer
    
    A_bar();  // since bar() is static, there is no need to pass the this pointer
    

    Once upon a time I would have guessed that this was handled with pointers-to-functions in each A object created. However, that approach would mean that every A object would contain identical information (pointer to the same function) which would waste a lot of space. It’s easy enough for the compiler to take care of these details.

    For classes/objects with non-virtual inheritance

    Of course, that wasn’t really what you asked. But we can extend this to inheritance, and it’s what you’d expect:

    class B : public A {
        void blarg()
        {
            // who knows, something goes here
        }
    
        int bar()
        {
            return 5;
        }
    };
    
    B b;
    b.blarg();
    b.foo();
    b.bar();
    

    The compiler turns the last four lines into something like:

    struct B b = <compiler-generated constructor>
    B_blarg(b);
    A_foo(b.A_portion_of_object);
    B_bar(b);
    

    Notes on virtual methods

    Things get a little trickier when you talk about virtual methods. In that case, each class gets a class-specific array of pointers-to-functions, one such pointer for each virtual function. This array is called the vtable (“virtual table”), and each object created has a pointer to the relevant vtable. Calls to virtual functions are resolved by looking up the correct function to call in the vtable.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look in django package: from django.utils import simplejson as json… May 14, 2026 at 2:04 pm
  • Editorial Team
    Editorial Team added an answer The problem is with this line: this.pushIn.bind(el) When pushIn is… May 14, 2026 at 2:04 pm
  • Editorial Team
    Editorial Team added an answer You assign pointer to the char, not char*. You read… May 14, 2026 at 2:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.