谁可以解释清楚下列代码,为什么输出是这些或者为什么不会崩溃
#include <iostream>
using namespace std;
class A
{
public:
A() { };
virtual void funcA()
{
std::cout << "funcA" << endl;
};
};
class B : public A
{
public:
int num;
B() {num = 10; };
virtual void funcA() override
{
std::cout << "funcA in B" << endl;
};
void funcB()
{
num = 100;
std::cout << "funcB" << endl;
}
void funcC()
{
std::cout << "funcC" << endl;
std::cout << num << endl;
}
};
int main(int argc, char* argv[])
{
A* pA = new A;
B* pB = (B*)(pA);
pB->funcA();
pB->funcB();
pB->funcC();
return 1;
}
输出是:
funcA
funcB
funcC
100