Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use virtual methods?

Tags:

I understand that virtual methods allow a derived class to override methods inherited from a base class. When is it appropriate/inappropriate to use virtual methods? It's not always known whether or not a class will be sub classed. Should everything be made virtual, just "in case?" Or will that cause significant overhead?

like image 867
Maxpm Avatar asked May 19 '11 13:05

Maxpm


People also ask

When would you use a virtual function?

Basically, a virtual function is used in the base class in order to ensure that the function is overridden. This especially applies to cases where a pointer of base class points to an object of a derived class.

What is the point of virtual methods?

Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. Functions are declared with a virtual keyword in base class.

What could be the advantages to use a virtual method?

The main advantage of virtual functions are that they directly support object oriented programming. When you declare a function as virtual you're saying that exactly what code is executed depends on the type of the object you call it against. you can't tell exactly what code path it's going to follow.

Why do we use virtual method in Java?

In Java Every non-static, non-final, and public method is a virtual function. These methods can be used to achieve polymorphism. The methods that can not be used to achieve the polymorphism never be virtual function. A static, final, and private method never be the virtual function.


2 Answers

First a slightly pedantic remark - in C++ standardese we call them member functions, not methods, though the two terms are equivalent.

I see two reasons NOT to make a member function virtual.

  • "YAGNI" - "You Ain't Gonna Need It". If you are not sure a class will be derived from, assume it won't be and don't make member functions virtual. Nothing says "don't derive from me" like a non-virtual destructor by the way (edit: In C++11 and up, you have the final keyword] which is even better). It's also about intent. If it's not your intent to use the class polymorphically, don't make anything virtual. If you arbitrarily make members virtual you are inviting abuses of the Liskov Substitution Principle and those classes of bugs are painful to track down and solve.
  • Performance / memory footprint. A class that has no virtual member functions does not require a VTable (virtual table, used to redirect polymorphic calls through a base class pointer) and thus (potentially) takes up less space in memory. Also, a straight member function call is (potentially) faster than a virtual member function call. Don't prematurely pessimize your class by pre-emptively making member functions virtual.
like image 178
Joris Timmermans Avatar answered Oct 04 '22 21:10

Joris Timmermans


When you design a class you should have a pretty good idea as to whether it represents an interface (in which case you mark the appropriate overrideable methods and destructor virtual) OR it's intended to be used as-is, possibly composing or composed with other objects.

In other words your intent for the class should be your guide. Making everything virtual is often overkill and sometimes misleading regarding which methods are intended to support runtime polymorphism.

like image 30
Mark B Avatar answered Oct 04 '22 21:10

Mark B