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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:54:39+00:00 2026-06-05T01:54:39+00:00

#include <iostream> using namespace std; class Base { public: Base() { cout << In

  • 0
#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "In Base" << endl;
        cout << "Virtual Pointer = " << (int*)this << endl;
        cout << "Address of Vtable = "
        << (int*)*(int*)this << endl;
        cout << "Value at Vtable = "
        << (int*)*(int*)*(int*)this << endl;
        cout << endl;
    }

    virtual void f1() { cout << "Base::f1" << endl; }
};

class Drive : public Base {
public:
    Drive() {
        cout << "In Drive" << endl;
        cout << "Virtual Pointer = "
        << (int*)this << endl;
        cout << "Address of Vtable = "
        << (int*)*(int*)this << endl;
        cout << "Value at Vtable = "
        << (int*)*(int*)*(int*)this << endl;
        cout << endl;
    }

    virtual void f1() { cout << "Drive::f2" << endl; }
};

int main() {
Drive d;
return 0;

}

The output of this program is

In Base
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C08C
Value at Vtable = 004010F0

In Drive
Virtual Pointer = 0012FF7C
Address of Vtable = 0046C07C
Value at Vtable = 00401217

Follow the code, i can see that when i create a Drive object, the Base constructor also run and show the address of virtual pointer same with Drive’s virtual pointer: 0012FF7C. The weird thing is that when i dereference that address in both constructors of Base and Drive class, it pointed to different value meaning there are two vtable, one at 0046C08C and another at 0046C07C. Its very difficult to understand in structure of Drive object and also in c langue when 1 pointer point 2 address.

  • 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-06-05T01:54:40+00:00Added an answer on June 5, 2026 at 1:54 am

    You’ve just witnessed how your C++ compiler implements the rules set out by the C++ standard. While the Base constructor is running, any calls to virtual methods need to be dispatched to the Base implementations, even if Drive overrides them. To make that happen, your C++ compiler evidently makes the object’s vtable pointer point to the Base vtable. When the Base constructor finishes and execution continues in the Drive constructor, the vtable pointer gets updated to point at the Drive vtable.

    That ends up being a convenient way to implement it. The rest of code, where the compiler generates instructions to call virtual methods, doesn’t need to change to detect whether the special constructor behavior is required. It can just look in the vtable like always. Changing the vtable pointer is a quick way to change the effective run-time type of the object while the constructors run.

    You’ll probably find the destructors work in a similar manner, but in reverse. If you construct a Base object instead of a Drive object, you’ll probably see similar addresses as you do in the Base constructor of your Drive object.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
#include <iostream> #include <conio.h> using namespace std; class Base { int a; public: Base(const
#include<iostream> using namespace std; class base { protected: int a; public: base(int i) {
#include <iostream> using namespace std; class Base { public: Base() { cout << In

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.