Most modifiers make sense to me - abstract, protected etc. But "virtual" seems a confusing arbitrary choice for what is really "overridable".
'Virtual' in computing (virtual machine, virtual memory) seems to mean an abstract representation of something. I guess this originated from optics, where a virtual image (usually in*vert*ed) itself comes from Medieval Latin virtuālis, meaning "effective".
So it seems to me it would make more sense if the 'virtual' method in computing was the overriding one, not the one-to-be-overriden.
Maybe I'm missing something fundamental about 'virtual', can anyone shed any more light on why it was chosen?
The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.
A 'virtual' is a keyword preceding the normal declaration of a function. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.
A virtual keyword is an indication to the compiler that a method may be overridden in derived classes. Coming to the C# perspective, the virtual keyword is used to modify the declaration of any property, method or event to allow overriding in a derived class.
The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.
In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class. In other words, it is not necessary to use the keyword virtual in the derived class while declaring redefined versions of the virtual base class function.
Which of the following cannot be used with the virtual keyword? Explanation: Virtual keyword cannot be used with constructors as constructors are defined to initialized an object of particular class hence no other class needs constructor of other class.
I tend to think of the term "virtual" as meaning not real. Such as, in terms of virtual memory, that maps through to real memory at some point but is really a placeholder until the mapping is needed.
Similarly, a virtual function is a placeholder which maps through to a real function at some point.
Even if you use the (somewhat childish) "pretend" definition, it makes sense:
virtual/pretend machine
virtual/pretend memory
virtual/pretend function
In all those cases, the "virtual" is something that isn't real. The use of them is translated into something real (emulating the machine, mapping the memory addresses through an MMU, calling a class-specific actual function rather than the virtual one, and so on).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With