Glossary: Virtual Function

When one class, say class B inherits from another class, say class A, effectively, every object of class B has an embedded object of class A. C++ allows messages to be passed to the A object even when it is embedded in B. For example, if ptrB is a pointer to an object B, then the statement:- A *ptrA = (A*) ptrB; creates a pointer to the embedded A. If both classes A and B have a member function of the same name, they remain distinct unless they are declared as virtual. In this case, when an embedded A receives the message, it passes it onto B for processing. The B object can still access A's member function if needed, but the user sending the message to A has no such choice. This mechanism allows class B to modify the behaviour of the base class A, a phenomenon that can give rise to polymorphism.

For example the ROOT class TObject has a virtual member function Paint, which classes that inherit from it can modify.

See OO Concepts:Virtual Functions & Polymorphism and Interfaces & Abstract Classes
Go Back to the The Glossary Top Page

If you have any comments about this page please send them to Nick West