Virtual Table

I am trying to draw virtual table for following classes:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  class Base
{
public:
    virtual void function1() {};
    virtual void function2() {};
};
 
class D1: public Base
{
public:
    void function1() {};
};
 
class D2: public Base
{
public:
    void function2() {};
};



And I get-
for class Base Virtual table contains
pointer1 for function1()
pointer2 for function2()

for class D1 Virtual table contains
pointer1 for function1() of base class
pointer2 for function2() of base class

for class D2 Virtual table contains
pointer1 for function1() of base class
pointer2 for function2() of base class

Can any one correct me if I did anything wrong here?
And if its correct then how Compiler use Virtual Table for resolving late binding/Dynamic Polymorphism?
Last edited on
Please reply if any one can explain on Virtual Table.
waiting for reply...
Can any one correct me if I did anything wrong here?

Looks correct, although there's nothing that says it needs to be implemented that way.


And if its correct then how Compiler use Virtual Table for resolving late binding/Dynamic Polymorphism?

By using said pointers instead of invoking functions based on the static type of the object.
Thank you cire for reply.

As you said "By using said pointers instead of invoking functions based on the static type of the object."

but as i mentioned above for both class D1 and D2,
virtual table contains pointer of function which pointing to Base class function only not to D1/D2 class function then how its possible for compiler to know which function should call?
for class Base Virtual table contains
pointer1 for function1()
pointer2 for function2()

for class D1 Virtual table contains
pointer1 for function1() of base class
pointer2 for function2() of base class

for class D2 Virtual table contains
pointer1 for function1() of base class
pointer2 for function2() of base class
Wrong! It should be
1
2
3
4
5
6
7
8
9
10
11
for class Base Virtual table contains
pointer1 for function1()
pointer2 for function2()

for class D1 Virtual table contains
pointer1 for function1() of D1 class
pointer2 for function2() of base class

for class D2 Virtual table contains
pointer1 for function1() of base class
pointer2 for function2() of D2 class
Hypothetical:

Lets say that you have memory block "Base" that contains a "vtable" pointer and a fixed array of function pointers (for Base::f1 and Base::f2).
Then you have block "D1" that has its own pointer array (with D1::f1, Base::f2, and D1::f3), and a block that contains "Base" bytes.

When you create object of type Base, things are simple. Calling f1() dereferenses vtable to get to the Base::table and within there to Base::f1.

When you create object of type D1, initialize the Base block as normal, initialize the D1::table, and then update the vtable to point to the D1::table.

Now you have that polymorphic Base * p = &D1_object; and you call p->f1()
The vtable pointer directs to D1::table, which directs to D1::f1. That is why the runtime knows.

Attempt to call p->f3() is stopped by the compiler because p could be a genuine Base during runtime and Base does have only two entries in its table.
Thank you miinipaa for reply.

But as you said,

for class D1 Virtual table contains

pointer1 for function1() of D1 class : but function1() in class is not a virtual , so that's my concern is how it should be in VTable.

pointer2 for function2() of base class : Its understood

for class D2 Virtual table contains
pointer1 for function1() of base class :Its understood
pointer2 for function2() of D2 class : same as D1. In D2 also function2() is not a virtual.


My doubt is this only in above case we didnt declare virtual to function1() and function2() in class D1 and D2 resp. Then how should be function pointer for this function in their resp VTables.
Because we know that VTable only contains function pointer for Virtual functions.

Please reply-
If function declared as virtual in base class, then it will be virtual in derived, no matter if you do declare them virtual there or not. It is better to explicitly state that they are virtual to avoid confusion but you should know, that once something declares function virtual, it stays virtual in all derived classes.
but function1() in class is not a virtual

Yes it is. Once a method is declared as virtual in a base class, it is virtual in all subclasses. It doesn't matter whether you use the keyword virtual in the definition of those methods in the subclasses - the methods will still be virtual.

In fact, it's recommended that you use the virtual keyword even though it's not necessary, to make the code as self-documenting as possible.

EDIT: Ninja'd.
Last edited on
hey thanks very much to all of you guys.

MiiniPaa and MikeyBoy you replayed what actually i was looking for.

Thanks.
Topic archived. No new replies allowed.