What is the difference between these two:
- Declaring the base class function virtual and changing the derived class function.
- Overloading an inherited non-virtual function.
When would you use one over the other?
What is the difference between these two:
When would you use one over the other?
When you have a Base class method declared as virtual
, In order to override it you need to provide an function with exact same signature in Derived class(Co-variant return types are allowed though).
If your function name is same but the signature in Derived class varies from one in Base class than it is not overidding anymore, It is function Hiding, the derived class method hides the Base class method.
Function Overloading is never accross classes, You can overload methods inside the same class or free functions but not accross classes. When you attempt to do it accross classes what you eventually get is function hiding.
To bring the Base class methods in scope of your Derived class you need to add an
additional using functionName
, to your Derived class.
EDIT:
As for the Q of when to use virtual
over overloading,the answer is:
If you intend functions of your class to be overridden for runtime polymorphism you should mark them as virtual
, and not if you don't intend so.
Good Read:
When to mark a function in C++ as a virtual?
Overloading is completely separate from (orthogonal to) virtual overriding.
In overriding, one function is replaced with another of an identical signature. There is then some rule to pick the "most overriding" function, which for virtual functions means the one defined in the most-derived class. As a special case for virtual functions, the return types of the signatures may differ slightly (covariance).
In overloading, function signatures with different argument types simultaneously act as candidates to be chosen when you make a function call. There is an incredibly complicated set of rules to pick the right one, which works well 95% of the time and gives you a headache when it doesn't cooperate.
Since overloading works with different signatures and overriding works with same signatures, they don't really interfere with each other.
You can explicitly import the functions of a base class into a derived class in order to extend an overloaded function name. This is done by using base_class::overload_name;
inside the derived class.
I believe you meant overriding the non-virtual functions and not overloading. When you override a non-virtual base class function in the derived class, then the call to the function is resolved and bound at compile time. This means that the function call is resolved based on the type (or pointer) on which the function is called. If you are calling the function on the base class pointer then the base class version is called, always. If you use the derived class pointer then the derived version is called, always; irrespective of the actual object that it is pointing to.
In case the base class version is marked as virtual then the call resolution or binding is deferred to be made at runtime based on the type of object on which the call is made and not based on the type of pointer being used to make the call. This means you can use the base class pointer to point to base and derived class objects and then call the function. Based on the type of object the pointer is pointing to, the respective function version is called. This means that if my pointer is pointing to base class object then the base class version is called. If the pointer points to derived type object then the derived verion is called.