Home / def / virtual 
virtual
A virtual class is a class which represents a sub-component of a class, but which you cannot instanciate nor reference into a variable.

Components written in C/C++

In native components, virtual classes are a mechanism designed so that the user manipulates temporary objects, without being having to create them. It is SO much faster!

Note that the name of a virtual class must begin with a dot. For example, the name of the virtual class used by the ListView.Item property is .ListViewItem.

Example

The property Item of the Qt component ListView class uses a virtual class for representing a ListView item.

Virtual classes are just used as datatypes by the interpreter. But the object used behind is the real object coming from the real non-virtual class.

For example, the Item property of the ListView class stores the index of the item you want to deal with in the ListView object, and returns this ListView object. The ListView object becomes then a virtual class object that you cannot store in a variable. As you must use the virtual class object immediately, by calling a method or a property on it, the stored index will be used immediately too.

Components written in Gambas

In components written in Gambas, virtual classes are classes whose name begins with an underscore.

They are actually true classes that work exactly like normal classes, and so they are not faster in any way.