virtual tables

Can i access the virtual tables explicitly ??? According to my understanding the virtual table is an array of pointer to functions. So can i like get the base addresses of the functions by using virtual tables themselves ??
Please Help.
Thanks in advance.
Why?
What do you want to do with the table??

As a Part of an assignment i m supposed to find the address of the virtual function using ONLY virtual table.
I m not supposed to use the base address of the object to that .
it's not a virtual table, it's a table of pointers to virtual functions.
you shouldn't try to access them.
if you think that you need to, you'll probably find the pointer to a table in the first 4 bytes of your object.
what is a "base address of a function" ?
don't start multiple threads about the same thing.
You are being asked to do something very odd...

The reason is that the virtual table is stored and organized in a compiler-specific way. Even different versions of the same compiler may store it differently. There is no uniform method. That is, the way the virtual table is handled is up to the compiler.

[edit] IIRC
Last edited on
Unless this is a compiler-construction, operating systems architecture, hardware architecture, or assembly class, you probably interpret the assignment incorrectly.

You can not find the address of anything virtual without using objects unless you know the area in memory where the compiler stores the class meta-data and this is job for a black-hat hacker.

Best regards
Thank you all for your help. I guess there is something wrong with the way the assignment question was phrased. I ll check with my instructor tomorrow.

"it's not a virtual table, it's a table of pointers to virtual functions."

i do know what i t means as i have written in my original post :

"According to my understanding the virtual table is an array of pointer to functions."

I m sorry about "base address of a function" thing. My bad.What i meant was the address of the virtual function.

And i m sorry about posting multiple threads for the same problem.I did not get any reply for the post in the beginners section so posted it here .

kind regards ,

Parminder
Last edited on
It is still unclear.

The "address" of a virtual function by itself (ie, without an instance of the object) isn't
a real memory address, the reason being that:

1
2
3
4
5
6
7
8
9
10
11
struct Base {
    virtual void foo();
};

struct Intermediate : Base {
    virtual void foo();
};

struct Derived : Intermediate {
    virtual void foo();
};


when you say "&Derived::foo", "&Intermediate::Foo", or "&Base::foo", the compiler
does not know which function you want the address of, despite the qualification.
Because, this has to work:

1
2
3
4
5
int main() {
    Derived* d = new Derived;
    void (Base::*pFn)() = &Base::foo;   // pointer to Base::foo
    d->*pFn(); // Calls Derived::foo by pointer
}


On line 3, the compiler does not know how you are going to use pFn to know that
you really meant the address of "Base::foo" or if you meant one of its overrides.

On gcc, you'll find that sizeof( pFn ) is in fact more than 32 bits even on a 32-bit
system; the reason is because the pointer encodes two additional pieces of
information: it encodes the fact that the pointer is a pointer to a virtual function
and it encodes the index of the virtual function into the vtable. (In my above bad
example, foo() is the 0th virtual function the compiler encountered in the class).
Topic archived. No new replies allowed.