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 6623271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:48+00:00 2026-05-25T21:30:48+00:00

class base { public: virtual void fn(){} }; class der: public base { public:

  • 0
class base {
public:
  virtual void fn(){}
};


class der: public base {
public:
  void fn(){}
};

der d;

base *b = &d;
b->fn();

When the compiler encounters the statement b->fn(), the following information is available to the compiler:

  1. b is a pointer to the class base,
  2. base class is having a virtual function as well as a vptr.

My question is: how does the vptr of class der come into picture at run time?

  • 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-25T21:30:49+00:00Added an answer on May 25, 2026 at 9:30 pm

    When reasoning about this, it helps me to maintain a clear image of the memory layout of the classes, and in particular to the fact that the der object contains a base subobject that has exactly the same memory layout as any other base object.

    In particular your base object layout will simply contain a pointer to the vtable (there are no fields), and the base subobject of der will also contain that pointer, only the value stored in the pointer differs and it will refer to the der version of the base vtable (to make it a bit more interesting, consider that both base and der did contain members):

    // Base object         // base vtable (ignoring type info)
    +-------------+        +-----------+
    | base::vptr  |------> | &base::fn |
    +-------------+        +-----------+
    | base fields |
    +-------------+
    
    // Derived object      // der vtable
    +-------------+        +-----------+
    | base::vptr  |------> | &der::fn  |
    +-------------+        +-----------+ 
    | base fields |
    +-------------+ <----- [ base subobject ends here ]
    | der fields  |
    +-------------+
    

    If you look at the two drawings, you can recognize the base subobject in the der object, when you do base *bp = &d; what you are doing is obtaining a pointer to the base subobject inside der. In this case, the memory location of the base subobject is exactly the same as that of the base subobject, but it need not be so. What is important is that the pointer will refer to the base subobject, and that the memory pointed to has the memory layout of a base, but with the difference that the pointers stored in the object will refer to the der versions of the vtable.

    When the compiler sees the code bp->fn(), it will consider it to be a base object, and it knows where the vptr is in a base object, and it also knows that fn is the first entry in the vtable, so it only needs to generate code for bp->vptr[ 0 ](). If bp refers to a base object then bp->vptr will refer to the base vtable and bp->vptr[0] will be base::fn. If the pointer on the other hand refers to a der object, then bp->vptr will refer to the der vtable, and bp->vptr[0] will refer to der::fn.

    Note that at compile time the generated code for both cases is exactly the same: bp->vptr[0](), and that it gets dispatched to different functions based on the data stored in the base (sub)object, in particular the value stored in vptr, which gets updated in construction.

    By clearly focusing on the fact that the base subobject must be present and compatible with a base object you can consider more complex scenarios, as multiple inheritance:

    struct data { 
       int x;
    };
    class other : public data, public base {
       int y;
    public:
       virtual void fn() {}
    };
    +-------------+
    | data::x     |
    +-------------+ <----- [ base subobject starts here ] 
    | base::vptr  |
    +-------------+
    | base fields |
    +-------------+ <----- [ base subobject ends here ]
    | other::y    |
    +-------------+
    int main() {
       other o;
       base *bp = o;
    }
    

    This is a more interesting case, where there is another base, at this point the call base * bp = o; creates a pointer to the base subobject and can be verified to point to a different location than the o object (try printing out the values of &o and bp). From the calling site, that does not really matter because bp has static type base*, and the compiler can always dereference that pointer to locate base::vptr, use that to locate fn in the vtable and end up calling other::fn.

    There is a bit more magic going on in this example though, as the other and base subobjects are not aligned, before calling the actual function other::fn, the this pointer has to be adjusted. The compiler resolves by not storing a pointer to other::fn in the other vtable, but rather a pointer to a virtual thunk (small piece of code that fixes the value of this and forwards the call to other::fn)

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

Sidebar

Related Questions

I'm having the following classes: class Base { public virtual void Print() { Console.WriteLine(Base);
Given the following snippet, class Base { public: virtual void eval() const { std::cout<<Base
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
consider the following program: class Base { public: virtual void foo() const { cout
I have a question, here are two classes below: class Base{ public: virtual void
Given a base class with the following interface: public class Base { public virtual
class Base { public: virtual void foo() const { std::cout << Base; } };
In C++, you can do the following: class base_class { public: virtual void do_something()
class Base { public: virtual void foo() {} }; class Derived: public Base {
Suppose I have a base and derived class: class Base { public: virtual void

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.