Wednesday, October 19, 2011

Can Virtual Functions Be Overloaded?

Polymorphism and virtual functions are powerful strategies in C++ for object-oriented programming. Also, function overloading is very convenient if multiple functions work similarly but take different parameters. So, can these two happen along with each other? If yes, how will this work?

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:
  1. Virtual functions can be overloaded in base or derived classes, like the code lines 6 - 16.
  2. The qualifier "virtual" is not part of function signature, and therefore 2 functions with "virtual" difference only are not allowed by compiler.
  3. 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.
  4. Pure virtual functions can also be overloaded in the abstract class body, like the code lines 25 - 27.
  5. Pure virtual functions can also be overloaded by regular virtual functions or non-virtual functions, examples not shown in the given code.
  6. 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.
  7. 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.
  8. 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.
  9. 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).
  10. 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.
  11. Parameter types may be automatically casted to the type declared in the function parameter list, like the code lines 102 and 106.
  12. Default arguments work normally for virtual functions, as in regular functions, like the code lines 111 - 114 and 116 - 119.
  13. Ambiguous function calls are not allowed by compiler. Code line 126 will cause a compile error if uncommented.
  14. Default arguments will be ignored if functions are called via base class type pointers or references, like the code line 139.

1 comment:

  1. There is no source code. Without it, the output is meaningless, as is most of this article.

    ReplyDelete