Saturday, April 17, 2021

override , final, virtual (function) keyword

override, final, virtual (function) keyword

    keyword:final

  1. final specifier (since C++11)
  2. Definition: Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

    Usage:

    declarator virt-specifier-seq(optional) pure-specifier(optional) (1)

    declarator virt-specifier-seq(optional) function-body (2)

    class-key attr(optional) class-head-name class-virt-specifier(optional) base-clause(optional) (3)

    1. In a member function declaration, final may appear in virt-specifier-seq immediately after the declarator, and before the pure-specifier, if used.
    2. In a member function definition inside a class definition, final may appear in virt-specifier-seq immediately after the declarator and just before function-body.
    3. In a class definition, final may appear as class-virt-specifier immediately after the name of the class, just before the colon that begins the base-clause, if used.

    In the cases (1,2), virt-specifier-seq, if used, is either override or final, or final override or override final. In the case (3), the only allowed value of class-virt-specifier, if used, is final

    Syntax

    When applied to a member function, the identifier final appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition.

    When applied to a class, the identifier final appears at the beginning of the class definition, immediately after the name of the class.

    Explanation

    When used in a virtual function declaration or definition, final specifier ensures that the function is virtual and specifies that it may not be overridden by derived classes. The program is ill-formed (a compile-time error is generated) otherwise.

    When used in a class definition, final specifies that this class may not appear in the base-specifier-list of another class definition (in other words, cannot be derived from). The program is ill-formed otherwise (a compile-time error is generated). final can also be used with a union definition, in which case it has no effect (other than on the outcome of std::is_final) (since C++14), since unions cannot be derived from.

    final is an identifier with a special meaning when used in a member function declaration or class head. In other contexts it is not reserved and may be used to name objects and functions.

    keyword:override

  3. override specifier (since C++11)
  4. Definition:Specifies that a virtual function overrides another virtual function..

    Syntax The identifier override, if used, appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition.

    Usage:

    declarator virt-specifier-seq(optional) pure-specifier(optional) (1)

    declarator virt-specifier-seq(optional) function-body (2)

    1. In a member function declaration, override may appear in virt-specifier-seq immediately after the declarator, and before the pure-specifier, if used.
    2. In a member function definition inside a class definition, override may appear in virt-specifier-seq immediately after the declarator and just before function-body.

    In both cases, virt-specifier-seq, if used, is either override or final, or final override or override final.

    Explanation

    In a member function declaration or definition, override specifier ensures that the function is virtual and is overriding a virtual function from a base class. The program is ill-formed (a compile-time error is generated) if this is not true.

    override is an identifier with a special meaning when used after member function declarators: it's not a reserved keyword otherwise.

    virtual function:

  5. virtual function specifier
  6. Definition:The virtual specifier specifies that a non-static member function is virtual and supports dynamic dispatch. It may only appear in the decl-specifier-seq 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 qualified name lookup (that is, if the function's name appears to the right of the scope resolution operator ::).

    In detail

    If some member function vf is declared as virtual in a class Base, and some class Derived, which is derived, directly or indirectly, from Base, has a declaration for member function with the same

    1. name
    2. parameter type list (but not the return type)
    3. cv-qualifiers
    4. ref-qualifiers

    key point: Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).

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

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

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

    Key Note: Non-member functions and static member functions cannot be virtual.

    Key Note: Function 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.

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

    Moreover, if a class is polymorphic (declares or inherits at least one virtual function), and its destructor is not virtual, deleting it 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

    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 initializer list), 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.

    My Note: this example is great and contains knowledge...

    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:

  7. Can a class member function template be virtual?
  8. I have heard that C++ class member function templates can't be virtual. Is this true?

    If they can be virtual, what is an example of a scenario in which one would use such a function?

No comments:

Post a Comment