0
class Base
{
public:
    virtual void function1() {};
    virtual void function2() {};
};

class D1: public Base
{
public:
    virtual void function1() {};
};

class D2: public Base
{
public:
    virtual void function2() {};
};

for the above example, 3 V.tables will be create (source).

If I remove virtual keyword for the functions in D1, D2 will this code create 3 v.tables still?

Flexo
  • 87,323
  • 22
  • 191
  • 272
NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • @mat your edit missed a few bits, see the other things I fixed – Flexo Nov 07 '14 at 10:37
  • vtables are implementation-dependent, and a smart implementation will generate only two. That's because `Base::vtable` can overlap one of the derived vtables. A theoretical compiler can even create a single vtable here. S the C++ standard says **nothing** about vtables, it's perfectly OK to have holes in the vtable of D2. – MSalters Nov 07 '14 at 10:41

1 Answers1

1

First if a member function is declared as virtual in base then it would be virtual in all derived classes whether you explicitly state that or not.( Although it's implementation dependant)

Second each class having at least one function has one and only one vtable for it. So, for your question even if you remove virtual from your derived class functions they would remain virtual and there would be three vtables one for each class ( ON MOST OF IMPLEMENTATIONS).

ravi
  • 10,994
  • 1
  • 18
  • 36