How is overloading a virtual method different from a non-virtual method?

What is the difference between the two:

  • Declaring a base class function virtual and modifying a derived function class.
  • Overloading an inherited non-virtual function.

When do you use one over the other?

+7
source share
3 answers

If you have a method of the Base class declared as virtual , to override it you need to provide a function with the same signature in the Derived class ( options for returning covariants are allowed).

If your function name is the same, but the signature in the Derived class is different from the one in the base class, and it no longer pays for it, this is the function Hiding , the method of the derived class hides the method of the Base class.

The Overload function is never performed by class . You can overload methods within a single class or free functions, but not through classes. When you try to do this in classes, what you end up with is hiding the function.

To bring the base class methods to the scope of the Derived class, you need to add add using functionName to your Derived class.

EDIT:
As for Q, when to use virtual for overloading, the answer is:
If you intend to override the functions of your class for run-time polymorphism , you should mark them as virtual , and not if you do not intend to.

Good reading:
When to mark a function in C ++ as virtual?

+6
source

Congestion is completely separate from (orthogonal) virtual redefinition.

When overridden, one function is replaced with another identical signature. Then there is some rule for choosing the "most redefining" function, which for virtual functions means one that is defined in the derived class itself. As a special case for virtual functions, the returned signature types may be slightly different (covariance).

When overloading function signatures with different types of arguments, they simultaneously act as candidates for selection when the function is called. There is an incredibly complex set of rules to choose the right one that works well 95% of the time and gives a headache when it is not working.

Since overloading works with different signatures and redefines work with the same signatures, they do not interfere with each other.

You can explicitly import base class functions into a derived class to extend the overloaded function name. This is done using base_class::overload_name; inside a derived class.

+3
source

I believe that you were referring to overriding not virtual functions, not overloading. When you redefine a function of a non-virtual base class in a derived class, then the function call is allowed and bound at compile time. This means that a function call is permitted based on the type (or pointer) that the function is called on. If you call a function in the base class pointer, the version of the base class is always called. If you use a pointer to a derived class, then the derived version is always called; regardless of the actual object that it points to.

If the base class version is marked as virtual, then call resolution or binding is deferred at run time based on the type of object on which the call is made, and not on the basis of the type of pointer used to create the call. This means that you can use the base class pointer to specify the base and derived class objects, and then call the function. Based on the type of object that the pointer points to, the corresponding functional version is called. This means that if my pointer points to an object of a base class, then the version of the base class is called. If the pointer points to an object of a derived type, then the derived operator is called.

0
source

All Articles