What is a V-Table, what does it do? From my understanding, it stores function pointers, but why? Why wouldn't the compiler just copy-paste the parent functions into the current class?
For example, with the parent class:
#include <iostream>
class ParentClass
{
public:
int parentState;
virtual void func()
{
parentState = 1;
std::cout << "Parent Function: " << parentState << std::endl;
};
};
And a child class:
#include <iostream>
class ChildClass : public ParentClass
{
public:
void func()
{
parentState = 2;
std::cout << "Child Function: " << parentState << std::endl;
};
};
Why wouldn't the compiler just copy-paste everything from the parent class except the things that are overridden? Like this:
#include <iostream>
// "Compiled" or "Behind the Scenes" Child Class
class ChildClass
{
public:
int parentState;
void func()
{
parentState = 2;
std::cout << "Child Function: " << parentState << std::endl;
};
};
I may be greatly misunderstanding how C++ inheritance works, but it seems strange to me that a V-Table is even required. I have also heard that V-Tables are not the most performant thing, is this true?