-3

Lets assume we have the following code.

class b 
{ 
  public:   
  virtual foo(); 
}; 

class c : public b 
{ 
  public: 
  void foo(); 
};  

int main()
{
    C c;
    B* b = &c;
    b->foo(); //I understand that it call foo() in class C
}

What I want to understand is with the help of vtable ?

I understand the following things

1) b will have a vtable with pointer to function foo().

2) when the compiler comes across class c, vtable of b will be inherited in C.

My question

1) will the inherited vtable from b be updated the address of c::foo() ?. I have this doubt because c::foo() is not a virtual function. Predominantly what I have read points to vtable having virtual function.

Sathish
  • 227
  • 3
  • 11

1 Answers1

0

The 'virtual' of foo() is inherited.

Add unique cout to each foo() to confirm:

class B
{
public:
  virtual ~B() {}
  virtual void foo() { std::cout << "b::foo()" << std::endl; };
};

class C : public B
{
public:
   virtual ~C() {}
   virtual void foo() { std::cout << "c::foo()" << std::endl; };
};


int t205(void)
{
   C c;
   c.foo();

   B* b = &c;
   b->foo(); 

   b->B::foo();  // b::foo is still accessible

  return(0);
}

output:

c::foo()
c::foo()
b::foo()
2785528
  • 5,438
  • 2
  • 18
  • 20