Calling Virtual function from V-table [closed]
Asked Answered
T

4

7

As all the virtual function in C++ is stored in V-table. Overiding takes place in the case of virtual function. I want to ask there is any way by which we can call the Virtual function directly from the table and able too determined what functions V-table contains.

Touchmenot answered 14/6, 2013 at 10:29 Comment(2)
Why should one want to do that?? At least you can (using some direct memory addressing), as long you know about the inernals of your compiler ABI.Uncurl
can we do something with V-pointer that is present in Class(if the class contains the Virtual functions) which points to v-table.using g++ compiler, i tried with the V-pointer but its not working.Touchmenot
W
19

Well, actually you can. I don't care about portability, but in VS you can do it. Assuming that we are building 32-bit code with VS, the first 4 bytes at the objects address is the vtable address. By looking at the header files we know the order of methods in the vtable.

Example:

class Base
{
public:

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

class Derived : public Base
{
public:

    void printMessage()
    {
        std::cout << "Derived::printMessage()" << std::endl;
    }
};

int main(int argc, char* argv[])
{
    Derived d;

    unsigned int vtblAddress = *(unsigned int*)&d;

    typedef void(*pFun)(void*);

    pFun printFun = (pFun)(*(unsigned int*)(vtblAddress));

    printFun(&d);

    return 0;
}

P.S. I'm not going to ask why are you doing it, but here you have one option :-)

Wool answered 14/6, 2013 at 10:49 Comment(3)
this is actually i wanted.Touchmenot
Unbelievable, Peter Wood, Eelke, toro2k, Stony and quetzalcoatl are not able to tell what is being asked here. How come every participant in this thread understood what's being asked? 5 "Smart" guys with pretty low reputation compared to Mike Seymour and ForEveR are screwing everybody else. I mean, you don't understand the question, that's fine, but let the other people discuss it for god's sake...Wool
@user1764961- yaa really unbelievable man !!Touchmenot
W
2

There is no guarantee by standard, that virtual functions are implemented using v-table. So, only if you are sure, that compiler use v-table - you can find needed offset.

Whiten answered 14/6, 2013 at 10:33 Comment(4)
In c++ is it not necessary that compiler will use V-table for implementing Virtual functions???Touchmenot
@user2484070 yes, it's not necessary. Implementation of virtual functions is dependent on compiler.Whiten
@user2484070, the values will be stored somewhere and somehow, that's for sure. Is it going to be a well known V-table or something else... doesn't really matter.Wool
Yup !! now all thing clear to me. :-)Touchmenot
C
2

Portably, no. The language doesn't specify how virtual dispatch is implemented, only how it behaves. It is not necessarily implemented using a v-table, and there is no way to access a virtual function except to call it.

If you only need to support one particular ABI, then you could use the implementation details, along with some dodgy pointer casts, to map an object to a function pointer in the same way that the virtual dispatch mechanism does. But you'll be stepping outside the defined language into unsupported, non-portable territory, so I would definitely recommend rethinking whatever you're trying to do.

Clerestory answered 14/6, 2013 at 10:33 Comment(0)
A
0

I'd say no in general, because the exact implementation of the vtable is a platform/compiler specific. If you know how the platform/compiler implements the vtable and the addresation, it might be possible to compute it by determining the address of a vtable for a specific class and then adding a offset of a virtual method.

The vtable contains all the virtual methods for the class. You can disassemble the application to see them.

Ama answered 14/6, 2013 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.