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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:10:11+00:00 2026-05-13T07:10:11+00:00

When exactly does the compiler create a virtual function table? 1) when the class

  • 0

When exactly does the compiler create a virtual function table?

1) when the class contains at least one virtual function.

OR

2) when the immediate base class contains at least one virtual function.

OR

3) when any parent class at any level of the hierarchy contains at least one virtual function.

A related question to this:
Is it possible to give up dynamic dispatch in a C++ hierarchy?

e.g. consider the following example.

#include <iostream>
using namespace std;
class A {
public:
  virtual void f();
};
class B: public A {
public:
  void f();
};
class C: public B {
public:
  void f();
};

Which classes will contain a V-Table?

Since B does not declare f() as virtual, does class C get dynamic polymorphism?

  • 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-13T07:10:11+00:00Added an answer on May 13, 2026 at 7:10 am

    Beyond “vtables are implementation-specific” (which they are), if a vtable is used: there will be unique vtables for each of your classes. Even though B::f and C::f are not declared virtual, because there is a matching signature on a virtual method from a base class (A in your code), B::f and C::f are both implicitly virtual. Because each class has at least one unique virtual method (B::f overrides A::f for B instances and C::f similarly for C instances), you need three vtables.

    You generally shouldn’t worry about such details. What matters is whether you have virtual dispatch or not. You don’t have to use virtual dispatch, by explicitly specifying which function to call, but this is generally only useful when implementing a virtual method (such as to call the base’s method). Example:

    struct B {
      virtual void f() {}
      virtual void g() {}
    };
    
    struct D : B {
      virtual void f() { // would be implicitly virtual even if not declared virtual
        B::f();
        // do D-specific stuff
      }
      virtual void g() {}
    };
    
    int main() {
      {
        B b; b.g(); b.B::g(); // both call B::g
      }
      {
        D d;
        B& b = d;
        b.g(); // calls D::g
        b.B::g(); // calls B::g
    
        b.D::g(); // not allowed
        d.D::g(); // calls D::g
    
        void (B::*p)() = &B::g;
        (b.*p)(); // calls D::g
        // calls through a function pointer always use virtual dispatch
        // (if the pointed-to function is virtual)
      }
      return 0;
    }
    

    Some concrete rules that may help; but don’t quote me on these, I’ve likely missed some edge cases:

    • If a class has virtual methods or virtual bases, even if inherited, then instances must have a vtable pointer.
    • If a class declares non-inherited virtual methods (such as when it doesn’t have a base class), then it must have its own vtable.
    • If a class has a different set of overriding methods than its first base class, then it must have its own vtable, and cannot reuse the base’s. (Destructors commonly require this.)
    • If a class has multiple base classes, with the second or later base having virtual methods:
      • If no earlier bases have virtual methods and the Empty Base Optimization was applied to all earlier bases, then treat this base as the first base class.
      • Otherwise, the class must have its own vtable.
    • If a class has any virtual base classes, it must have its own vtable.

    Remember that a vtable is similar to a static data member of a class, and instances have only pointers to these.

    Also see the comprehensive article C++: Under the Hood (March 1994) by Jan Gray. (Try Google if that link dies.)

    Example of reusing a vtable:

    struct B {
      virtual void f();
    };
    struct D : B {
      // does not override B::f
      // does not have other virtuals of its own
      void g(); // still might have its own non-virtuals
      int n; // and data members
    };
    

    In particular, notice B‘s dtor isn’t virtual (and this is likely a mistake in real code), but in this example, D instances will point to the same vtable as B instances.

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

Sidebar

Ask A Question

Stats

  • Questions 426k
  • Answers 426k
  • 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 A bit of poking around in Firebug reveals that removing… May 15, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer Solved it. Based on all your answers, I suspected the… May 15, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer The winning answer goes to this forum post: Publish web… May 15, 2026 at 12:29 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.