Reputation: 149
I understand the concepts of vptr and vtable and their role in implementing this mechanism.
The compiler constructs a vtable for each class with at least one virtual function and adds a vptr pointer to each class instance so that it can access the vtable and execute the correct function in runtime, but I fail to understand how exactly.
Consider the following code:
struct A
{
virtual void foo() { cout << "A::foo" << endl; }
virtual void goo() { cout << "A::goo" << endl; }
};
struct B
{
virtual void goo() { cout << "B::goo" << endl; }
virtual void foo() { cout << "B::foo" << endl; }
};
int main()
{
A *r = (A *)(new B());
r->foo();
}
output:
B::goo
Could you explain how and why B::goo was executed? did the compiler determine the place to look for inside the vtable in compile time? Thanks.
EDIT:
The above code snippet is just an example that I thought would make you understand which part of the mechanism I don't understand - clearly, I was wrong :)
Please look at the following example:
struct A
{
virtual void foo() {/* some implementation */}
virtual void goo() {/* some implementation */}
};
int main()
{
A *a = new A();
a->foo(); // <=====
}
For this example the compiler constructs a vtable with two function pointers inside, one points to foo, the other to goo. When calling foo, what happens exactly? how does the compiler know which of the pointers(inside vtable) to use? hope this makes it clear.
Upvotes: 2
Views: 295
Reputation: 73206
Your example has undefined behaviour: you are violating the strict aliasing rule when trying to alias the B
object through an A
pointer.
As always with undefined behaviour, all bets are off, and any further analysis of the program behaviour is futile.
Upvotes: 7