virtual
keyword in C++ sounds bit different from other words like “Virtual Machines”, “Virtual Reality”.
- Virtual base classes make sure only one copy of a base class’s member variables are inherited by grandchild derived classes. Wikipedia – Virtual Inheritance
- Virtual functions are an inheritable and overridable function or method that is dispatched dynamically. Wikipedia – Virtual Function
Yeah, I understand what virtual
keyword does in those two cases. However the meaning of “Virtual” is related with those two cases? Virtual means something “not extisting, not real, happens online/inside computer world” and how is this related to virtual base classes/functions?
Example)
class Interface {
public:
virtual void foo() = 0;
};
class Impl: public Interface {
public:
void foo() override { ... }
};
For pure virtual functions,I can see they are “virtual” and derived(or interface implementations) classes should implement those non-existent functions in order to work.
class Animal {
public:
virtual void say() { cout << "I'm animal" << endl; }
};
class Dog: public Animal {
public:
void say() override { cout << "WOOF" << endl; }
};
But in this case, normal virtual functions, they have original forms(Saying “I’m animal”) and derived classes’ overriden ones are called dynamically on a runtime, which makes me think the virtual function doesn’t look “virtual” to me.