virtual function specifier
Specifies that a function is virtual
Contents |
[edit] Syntax
virtual function_declaration ;
|
|||||||||
[edit] Explanation
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. That means, even if a derived class is handled using pointer or reference to the base class, a call to a overridden virtual function would invoke the behavior defined in the derived class.
The function signature must be the same in order to be overridden.This section is incomplete Reason: handling of function signatures (member lookup rules) |
The return type of a overriding virtual function doesn't necessarily need to be the identical to that of the overridden function. The types can be different if they are covariant with each another. Two types are covariant if they satisfy the following requirements:
-
Here we refer to the overriding function as
Derived::f()
and to the overridden function asBase::f()
- both types are pointers or references to classes. Multi-level pointers or references are not allowed.
- the class of the return type of
Base::f()
must be a unambiguous and accessible direct or indirect base class of the class of the return type ofDerived::f()
. - the return type of
Derived::f()
must be equally or less cv-qualified than the return type ofBase::f()
.
If a virtual function is called directly, that is, explicitly qualifying the class it is a member of, then the virtual call mechanism is suppressed and that particular implementation is called.
A virtual destructor of a base class is always overridden by a destructor of a derived class, even though that destructors are otherwise not inherited.
The access rules to a virtual function are determined by the first declaration. Access rules defined by the declarations of the overriding functions apply only to the direct function calls.
virtual
function specifier implies membership, thus only member functions can be virtual. Also, since an instance of a class is needed in order to call a virtual function, virtual function can not be static.
Functions templates cannot be declared virtual
. This applies only to functions that are themselves templates - a regular member function of a class template can be declared virtual.
[edit] Example
class Parent { public: void functionA(); virtual void functionB(); //Note the keyword virtual void functionC(); }; class Child : public Parent { public: void functionA(); virtual void functionB(); //Note the keyword virtual }; int main() { Parent* p1 = new Parent; Parent* p2 = new Child; Child* c = new Child; p1->functionA(); //Calls Parent::functionA p1->functionB(); //Calls Parent::functionB p1->functionC(); //Calls Parent::functionC p2->functionA(); //Calls Parent::functionA because p2 points to a Parent p2->functionB(); //Calls Child::functionB even though p2 points // to a Parent because functionB is virtual p2->functionC(); //Calls Parent::functionC c->functionA(); //Calls Child::functionA c->functionB(); //Calls Child::functionB c->functionC(); //Calls Parent::functionC return 0; }
[edit] See also
- override specifier (since C++11)
- final specifier (since C++11)