cpp/language/virtual

The specifier specifies that a non-static  is virtual and supports dynamic dispatch. It may only appear in the of the initial declaration of a non-static member function (i.e., when it is declared in the class definition).

Explanation
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overriding behavior is preserved even if there is no compile-time information about the actual type of the class. That is to say, if a derived class is handled using pointer or reference to the base class, a call to an overridden virtual function would invoke the behavior defined in the derived class. Such a function call is known as virtual function call or virtual call. Virtual function call is suppressed if the function is selected using (that is, if the function's name appears to the right of the scope resolution operator ).

In detail
If some member function is declared as  in a class, and some class , which is derived, directly or indirectly, from , has a declaration for member function with the same Then this function in the class is also virtual (whether or not the keyword  is used in its declaration) and overrides Base::vf (whether or not the word  is used in its declaration).
 * name
 * parameter type list (but not the return type)
 * cv-qualifiers
 * ref-qualifiers

does not need to be accessible or visible to be overridden. ( can be declared private, or can be inherited using private inheritance. Any members with the same name in a base class of  which inherits  do not matter for override determination, even if they would hide  during name lookup.)

For every virtual function, there is the final overrider, which is executed when a virtual function call is made. A virtual member function of a base class  is the final overrider unless the derived class declares or inherits (through multiple inheritance) another function that overrides.

If a function has more than one final overrider, the program is ill-formed:

A function with the same name but different parameter list does not override the base function of the same name, but hides it: when examines the scope of the derived class, the lookup finds the declaration and does not examine the base class.

Non-member functions and static member functions cannot be virtual.

Function templates cannot be declared. This applies only to functions that are themselves templates - a regular member function of a class template can be declared virtual.

for virtual functions are substituted at the compile time.

Covariant return types
If the function overrides a function, their return types must either be the same or be covariant. Two types are covariant if they satisfy all of the following requirements:


 * both types are pointers or references (lvalue or rvalue) to classes. Multi-level pointers or references are not allowed.
 * the referenced/pointed-to class in the return type of must be an unambiguous and accessible direct or indirect base class of the referenced/pointed-to class of the return type of.
 * the return type of must be equally or less  cv-qualified than the return type of.

The class in the return type of must be either  itself, or must be a  at the point of declaration of.

When a virtual function call is made, the type returned by the final overrider is to the return type of the overridden function that was called:

Virtual destructor
Even though destructors are not inherited, if a base class declares its destructor, the derived destructor always overrides it. This makes it possible to delete dynamically allocated objects of polymorphic type through pointers to base.

Moreover, if the destructor of the base class is not virtual, deleting a derived class object through a pointer to the base class is undefined behavior regardless of whether there are resources that would be leaked if the derived destructor is not invoked.

A useful guideline is that the destructor of any base class must be public and virtual or protected and non-virtual, whenever delete expressions are involved.

During construction and destruction
When a virtual function is called directly or indirectly from a constructor or from a destructor (including during the construction or destruction of the class’s non-static data members, e.g. in a member ), and the object to which the call applies is the object under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class. In other words, during construction or destruction, the more-derived classes do not exist.

When constructing a complex class with multiple branches, within a constructor that belongs to one branch, polymorphism is restricted to that class and its bases: if it obtains a pointer or reference to a base subobject outside this subhierarchy, and attempts to invoke a virtual function call (e.g. using explicit member access), the behavior is undefined: