Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when "virtual" is in "class Foo : public virtual Bar" as opposed to "virtual void frob()"?

Tags:

c++

virtual

What does it mean when "virtual" is in "class Foo : public virtual Bar" as opposed to "virtual void frob()"?

For a given method there are 8 cases stemming from the presence or absence of virtual in the following three locations.

  1. A superclass's functions.
  2. The inheritance chain for this class.
  3. This classes functions.

I think I understand how numbers 1 and 3 interact but number 2 seems redundant. Is it? What am I not understanding?

like image 685
Samuel Danielson Avatar asked May 08 '09 17:05

Samuel Danielson


People also ask

Can virtual function be public?

virtual functions can be private . This is because private means that the function cannot be called by derived classes. It does not prevent the entry to the v-table being overwritten. This means that the both the base class and the derived class will have access to the overwritten virtual function.

What does it mean for a function to be virtual?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Can a free function be declared as virtual?

As i said the "virtual" comes for free on free functions, you dont have to declare it as virtual, the only thing you have to do is to put a & next to the parameter.


1 Answers

That's virtual inheritance, you do it when you know you'll be doing multiple inheritance. That page goes into way more detail.

like image 153
Steve M Avatar answered Nov 10 '22 13:11

Steve M