Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does overloading a Virtual method differ from a Non-Virtual method?

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?

like image 280
Dasaru Avatar asked Mar 18 '12 07:03

Dasaru


People also ask

What are virtual functions How do they differ from non-virtual member function?

Non-virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object. In contrast, virtual member functions are resolved dynamically (at run-time).

Can a non-virtual function be overridden?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

What is non-virtual class?

The non-virtual interface pattern (NVI) controls how methods in a base class are overridden. Such methods may be called by clients and overridable methods with core functionality. It is a pattern that is strongly related to the template method pattern.


2 Answers

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?

like image 104
Alok Save Avatar answered Oct 31 '22 16:10

Alok Save


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.

like image 39
Potatoswatter Avatar answered Oct 31 '22 16:10

Potatoswatter