#include <iostream>
using namespace std;
class Father {
public:
int type = 0;
};
class SonA : public Father {
public:
void Foo() {
cout << "Message from sonA\n";
}
SonA() {
type = 1;
}
};
class SonB : public Father {
public:
void Bar() {
}
SonB() {
type = 2;
}
};
Father *fa = new SonA();
int main() {
if (fa->type == 1) {
fa->Foo();
} else if (fa->type == 2) {
fa->Bar();
}
}
大致目的是讨论参数的类型然后分别处理,这段代码会报两个错,请问有其他方法实现吗