In fact, virtual functions, as other functions, CAN be overloaded within the class defining them, in addition to the override option for derived classes. Even for pure virtual functions, they can have multiple signatures in abstract classes! The caveat for overloading pure virtual functions is that every virtual function signature must be implemented in each derived class for instantiation. Otherwise, it will be a compile error.
I wrote a program to illustrate and test virtual functions, including their overriding and overloading. Also, I attached the output below the program (You may need click and enlarge it to see clear output). At the end, I summarized the overloading of virtual functions.
Source code:
Output (click and enlarge to see it clearer):
Summary:
- Virtual functions can be overloaded in base or derived classes, like the code lines 6 - 16.
- The qualifier "virtual" is not part of function signature, and therefore 2 functions with "virtual" difference only are not allowed by compiler.
- Virtual functions can be overloaded by other virtual functions, as well as non-virtual functions, like the code lines 18 - 20. Function vfoo(double,int) is not virtual, which can be demonstrated by the code lines 132 - 137 when i = 4. Only base class version of function vfoo(double,int) was executed.
- Pure virtual functions can also be overloaded in the abstract class body, like the code lines 25 - 27.
- Pure virtual functions can also be overloaded by regular virtual functions or non-virtual functions, examples not shown in the given code.
- Pure virtual functions need to be implemented in derived classes for instantiation. Compiler requires that each pure virtual function signature be implemented, not just the one with signature match.
- If a derived class of an abstract class implements the virtual function, its own child classes can access its implementation if the inheritance is public.
- If multiple overloaded virtual functions (including pure virtual functions) exist in base class, but only one or some of the function signatures are overridden in derived class, all other un-overridden (including non-virtual) function signatures will be hidden and unavailable in derived class, even the inheritance is public. If uncommenting any lines of 103, 104, 108, or 109, it will be a compile error.
- However, if none of such virtual functions are overridden in derived class, all overloaded versions are available in derived class if the inheritance is public (like the code lines 58 - 59).
- If calling functions via base class type pointers or references, all base class overloaded functions, if not overridden in derived class, are available, like the code lines 132 - 137.
- Parameter types may be automatically casted to the type declared in the function parameter list, like the code lines 102 and 106.
- Default arguments work normally for virtual functions, as in regular functions, like the code lines 111 - 114 and 116 - 119.
- Ambiguous function calls are not allowed by compiler. Code line 126 will cause a compile error if uncommented.
- Default arguments will be ignored if functions are called via base class type pointers or references, like the code line 139.
There is no source code. Without it, the output is meaningless, as is most of this article.
ReplyDelete