How does the vptr works and can point to vtables of 2 classes?
18:03 28 Apr 2026

I wanted to make perhaps something like virtual table and virtual pointer but manually for class in C++ and get a better understanding of virtual pointer and tables

But I encountered a problem with making the virtual pointer.

The class A has vtableA, and class B has vtableB. The __vptr in class B is inherited from class A, and constructor tries to redirect it to the vtableB, so with this pointer it will be possible to call the B's method implementation, not A's, even though the pointer to the object of class B is of A data type.

However, I am getting an error
"a value of type "void (B::**)()" cannot be assigned to an entity of type "void (A::**)()""

class A
{
public:
    // Imagine that this is a hidden virtual table of A,
    void(A::* vtableA[1])() = {&A::sound};

    // this is hidden pointer to virtual table of A,
    void(A::**__vptr)() = vtableA;

    A()
    {
        __vptr = vtableA;
    }

    /*virtual*/ void sound()
    {
        cout << "Method A: ???" << endl;
    }
};

class B : public A
{
public:
    // and this is hidden virtual table of B
    void (B::* vtableB[1])() = {&B::sound};

    B()
    {
        __vptr = vtableB; // error
    }

    void sound() // override
    {
        cout << "Method B: Meow" << endl;
    }
};

If it is probably impossible to redirect __vptr to point on the virtual table of derived class because the types do not match, how then the virtual pointer is implemented internally?

It has special data type or does not it at all, or is it not a regular pointer-to-function, and something else?

I think if only the __vptr would not depend on the data type or to whom the pointer to a method belongs, that may work as I think

vptr