detemine vtable address

Hi,
I am understanding vtable concept and having some specific question.Can you provide me input.

1. Can we print vtable and its function.
2. Can we determine how many vtable has been created. (Theory yes) but any progrma or command which give some more info.

can anyone provide me some input...
I don't think there is a standard way to access virtual function table. If you want to see the addresses, you could compile this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

struct S{
    int x;
    virtual void f(){}
};

int main(){
    S s;
    s.x = 5;
    std::cout << "size : " << sizeof(S) << "\n";
    void*** ptr = (void***)&s;
    std::cout << "address : " << ptr << "\n";
    std::cout << "bytes : " << *ptr << " and " << *(ptr+1) << "\n";
    std::cout << "address if vftable : " << *ptr << "\naddress of f : " << **ptr << "\n";
    std::cin.get();
    return 0;
}
though it is quite likely not to work.

What do you mean by printing a function?
'how many vtable has been created' ? You mean, how many functions are in it? Or how many classes have such tables? Well, you can't do either.
No./No.
The C++ standard doesn't mention anything about vtables, it's just a popular way to implement virtual functions and related features.

However, when vtables are used, the pointer to the vtable is usually the first thing in the memory layout of an object instance, so you can get the appropriate vtable that way.
Of course, that's non-portable and serves no real purpose anyway.

Edit: ah, too slow.
Last edited on
adding little bit more info...
'how many vatable has been created" : I intend to say when we declare virtual function and
and use in single/multiple/multilevel inheritance than how many vtable are created
a very simple example:

class base
{
public:
virtual void fun();
}

class derived : public base
{

}

In this case we get two virtual table ( as per theory). So can we have some program
or any command in gdb debugger which give me info about vtable creation.
How many vtable has been created?

Topic archived. No new replies allowed.