c++多态求助
  • 板块学术版
  • 楼主MichaelDickson
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/3/12 22:17
  • 上次更新2024/3/13 13:20:12
查看原帖
c++多态求助
475401
MichaelDickson楼主2024/3/12 22:17
#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();    //
	}
}

大致目的是讨论参数的类型然后分别处理,这段代码会报两个错,请问有其他方法实现吗

2024/3/12 22:17
加载中...