What is the meaning of a pointer to a virtual function?

61 Views Asked by At

I have such a piece of code:

struct X
{
    virtual void f(){}
    virtual void g(){}
    virtual void h(){}
};
int main()
{
    auto p = &X::f, q = &X::g, r = &X::h;
    X *x = new X;
    return 0;
}

I thought the pointer to a virtual function would save the offset of the function in its' vtable. But when I use gdb to look at it, I found that the pointer value has nothing to do with the actual address of the function:

value of pointers

address of virtual functions

So, what's the actual meaning of the pointers? I know that a pointer to a virtual function, its actual pointing depends on the actual resolution of the virtual function. But my confusion is - if the value of the pointer to a virtual function does NOT represent the offset in the vtable, then what does it represent? And how does the program get the actual function by the value?

0

There are 0 best solutions below