Virtual function and Virtual function table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
  public:
  void fun1(){}
  virtual void fun2(){}
  virtual void fun3(){}
};

class B: public A
{
  public:
  void fun4(){}
  void fun2(){}
  virtual fun5(){}
};


Virtual Function Table for class A have entries for below functions:
fun2
fun3

Virtual Function Table for class B have entries for below functions:
fun2
fun5


Please correct me and let me know if the entries are correct in Virtual function table for both classes A and B.

For class B, the virtual table contains all of the virtual functions for A plus the new one(s) for B.

B would have entries in it's vtable for fun2, fun3 and fun5

It is merely that the entry for fun3 points to the version in A because B didn't provide one.

To continue, if class C were derived from B, and if B provided a fun3 but nothing else, the vtable for C would contain entries for fun2, fun3 and fun5, but where fun3 points to C's version.

[ Edit: Niccolo is correct because fun4 is not declared as virtual ]
A small mis-statement in Noccolo's answer: B's vtable has entries for fun2, fun3, fun4, and fun5.
Last edited on
@dhayden, I seriously doubt that.

fun4 is not declared virtual, so there's no vtable entry for fun4
Thanks Niccolo and dhayden for quick reply.
Initially, I was doubtful about the entry of fun3 in B's virtual table and that's why I posted this query here.

I agreed with Niccolo that B's virtual table have entries - fun2, fun3 and fun5.

@dhayden,
I did not get about entry of fun4, could you please explain us.



Ah. My mistake. I'll edit my post.
Thank you All...! :)
Topic archived. No new replies allowed.