We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is a Virtual Function?

By Jessica Susan Reuter
Updated: May 17, 2024
Views: 11,011
Share

A virtual function is a function, defined in a superclass, that must be present in a subclass for that subclass to have a complete class definition. Virtual functions rely on an object-oriented programming paradigm called virtual inheritance, which is most commonly seen in C++ using the "virtual" keyword. To define a virtual function, two classes are necessary, a superclass and a subclass. The superclass is where the function is first declared, and possibly defined. The subclass is where the function is defined — or overridden, depending on whether the function was defined in the superclass.

The virtual function can be defined in one of two ways. First, it can be defined as a stub, in which it has an empty body and does nothing. Second, it could be defined as a pure virtual function, where it is defined as NULL in the superclass's header file.

There are advantages and disadvantages to both methodologies. Defining a function as a stub ensures that all subclasses have some implementation of it, even if it does nothing. If one forgets to override the function and implement it properly in a subclass, however, no errors or warnings will appear to point this out. Defining a pure virtual function, on the other hand, requires each subclass to have its own definition of the function, and errors will appear if this isn't the case.

Virtual functions, however, are subject to the same inheritance rules as non-virtual functions, so inheritance hierarchies with more than two levels might not require explicit definitions of virtual functions. For example, one can consider a class A that declares a virtual function, which is implemented in subclass B. Class B has a subclass of its own, class C. Class C does not require an explicit definition of class A's function, because it inherits the definition from class B. If necessary, class C could override class B's function, or it could override class B's function while also calling it.

At the other extreme, virtual functions don't have to be defined in a subclass if they are declared virtual in that subclass. For example, one can consider a class A that declares a virtual function and has two subclasses, B and C. In addition, one could imagine that class B has subclasses D and E, and subclass C has subclasses F and G.

Classes B through G all must have class A's virtual function defined somehow. If class B has an implementation of A's function, classes D and E don't need it to be redone. Perhaps C's subclasses need to implement A's function, but they both do something different, so defining the function in class C itself wouldn't be useful. In that case, the function may be declared virtual in class C, and an implementation is not necessary.

Virtual functions can be daunting to learn, but when used properly, they can reduce code duplication and make code much easier to understand in general. There are many pitfalls with virtual functions, however, especially concerning multiple inheritance. In multiple inheritance, it is possible for ambiguously defined virtual functions to conflict with each other, so they should be used with caution in that context.

Share
WiseGeek is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.

Editors' Picks

Discussion Comments
Share
https://www.wisegeek.net/what-is-a-virtual-function.htm
Copy this link
WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.

WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.