Just look at the following two class. When I call the functions in "main", what will happen when compiled and program running?
#include <iostream>
#include <string>
using namespace std;
class A{
public:
virtual void fun2(){cout<<"A::fun2"<<endl;}
};
class B : public A{
public:
void fun2(){cout<<"B::fun2"<<endl;}
};
int main() {
A *a = new B();
B *b = new B();
//What's the differences among the followings?
a->A::fun2();
b->A::fun2();
A::fun2();
return 0;
}
I know what the program to print, but I wonder why. I know there is a virtual function table in the object, but when I call
a->A::fun2()
, how it works? Since in the a or b's v-table, the fun2() will print B::fun(), How does the program get into the function A::fun2()?